Add Legenda to OSMdroid mapview with Pop-Up

时光毁灭记忆、已成空白 提交于 2019-12-11 15:06:25

问题


I have a map with custom tiles and I need to add an optional legenda to the site explaining the color code of the custom map.

I have now tried to achieve what I want by triggering a toast showing an image via a menu. But now I learnt that toast is only displaying 3.5 seconds max, but I would need something that stays overlayed onto the map until it's being tapped onto. Thus also does not disappear as the map in the background is being moved.

EDIT: Here is my attempt to use an pop-up. But once I click the menu the app "unfortunately stops".

Here's my menu:

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    //..
        case R.id.legenda:
            showLegenda();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

And this is how my popup looks right now.

private void showLegenda() {
    // TODO Add popup here
 /*   LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.toastlegenda,
                                   (ViewGroup) findViewById(R.id.relativeLayout1));

    Toast toast = new Toast(this);
    toast.setView(view);
    toast.show();*/

    LayoutInflater inflater = (LayoutInflater)
               this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            PopupWindow pw = new PopupWindow(
               inflater.inflate(R.layout.toastlegenda, null, false), 
               100, 
               100, 
               true);
            // The following line raises the error:
            pw.showAtLocation(this.findViewById(R.id.mapview), Gravity.CENTER, 0, 0);

}

Here is map_layout.xml where my R.id.mapview is coming from:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<org.osmdroid.views.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
</LinearLayout>

Is mapview not possible as root container for a popup? The mapview is part of a tabview. Would be great if someon could give me a hint what raises the error here.

Here's the toastlegenda.xml (stemming from an earlier attempt to have an agenda with toast)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/relativeLayout1"
  android:background="@android:color/transparent">

<TextView
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView1" android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="Test"
    android:gravity="center"
    android:textColor="@android:color/black">
</TextView>

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:src="@drawable/dcmr_legenda_bl"
    android:layout_below="@+id/textView1"
    android:layout_margin="5dip"
    android:id="@+id/imageView1">
</ImageView>

<TextView
    android:id="@+id/textView2"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="Test 1,2, 1,2..."
    android:gravity="center"
    android:layout_below="@+id/imageView1"
    android:textColor="@android:color/black">
</TextView>

</RelativeLayout>

and the entire error:

E/AndroidRuntime(15404): FATAL EXCEPTION: main
E/AndroidRuntime(15404): java.lang.NullPointerException
E/AndroidRuntime(15404):    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809)
E/AndroidRuntime(15404):    at nl.sense.demo.MapActivity.showLegenda(MapActivity.java:156)
E/AndroidRuntime(15404):    at nl.sense.demo.MapActivity.onOptionsItemSelected(MapActivity.java:122)
E/AndroidRuntime(15404):    at android.app.Activity.onMenuItemSelected(Activity.java:2502)
E/AndroidRuntime(15404):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:969)
E/AndroidRuntime(15404):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
E/AndroidRuntime(15404):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
E/AndroidRuntime(15404):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
E/AndroidRuntime(15404):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
E/AndroidRuntime(15404):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
E/AndroidRuntime(15404):    at android.view.View$PerformClick.run(View.java:14263)
E/AndroidRuntime(15404):    at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(15404):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(15404):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(15404):    at android.app.ActivityThread.main(ActivityThread.java:4441)
E/AndroidRuntime(15404):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15404):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(15404):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(15404):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(15404):    at dalvik.system.NativeStart.main(Native Method)

来源:https://stackoverflow.com/questions/13271458/add-legenda-to-osmdroid-mapview-with-pop-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!