osmdroid workaround for the classic markers overlapping

心不动则不痛 提交于 2019-12-02 03:18:46

I don't know if this will help you. I needed to create a CustomInfoBubble for my project. What I did was, to extend the InfoWindow default class, and pass to it my custom bubble layout. Something like this: http://mobiledevstories.wordpress.com/2014/03/01/osmdroid-bonus-pack-markers-with-clickable-infowindows/

My Java class MapCustomInfoBubble looks like this:

public class MapCustomInfoBubble extends InfoWindow {

    public MapCustomInfoBubble(MapView mapView) {
        super(R.layout.map_infobubble_black, mapView);//my custom layout and my mapView
    }

    @Override
    public void onClose() {
      //by default, do nothing
    }

    @Override
    public void onOpen(Object item) {
        Marker marker = (Marker)item; //the marker on which you click to open the bubble    

        String title = marker.getTitle();
        if (title == null)
                title = "";

        Button moreInfo = (Button)mView.findViewById(R.id.bubble_moreinfo);//the button that I have in my XML; 
        moreInfo.setText(title);
        moreInfo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
             gotoMoreInfoWindow();//custom method; starts another activity
            }
        });
    }

}

In my XML file, I have:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:background="@drawable/map_infobubble_black" >
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:orientation="horizontal" >
            <TextView android:id="@+id/bubble_title" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFF" 
                android:maxEms="17"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:text="Title" />
            <Button android:id="@+id/bubble_moreinfo" 
                android:background="@drawable/map_btn_moreinfo"
                android:visibility="visible"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_gravity="right"
                android:layout_weight="0" />
        </LinearLayout>
</LinearLayout>

Somewhere else in my code, I then use:

Marker wp = new Marker(mapView);
wp.setPosition(new GeoPoint(mylocation.getMyLocation()));
wp.setTitle(editTextName.getText().toString());
wp.setInfoWindow(new MapCustomInfoBubble(mapView));
mapView.getOverlays().add(wp);
mapView.invalidate();

In my code, I set the text on the button with the Marker's title. The Marker is the item on which I click. If you want to put info about more markers in the same InfoWindow (inside a ListView), I think you would need to know in advance what the info will be.

I believe that, You can put whatever code you want inside onOpen(), however, I am not so sure if it's a good practice. You could try creating a custom Constructor and put your logic there. It should work. You need to pass the Resource Id (layout) and mapView to the super constructor, so it returns a valid mView object.

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