osmdroid解决方案为经典标记重叠

时间:2014-03-30 17:08:28

标签: android map osmdroid

我正在使用osmdroid和osm bonus pack开发Android离线地图应用程序,并从外部存储加载磁贴和数据。现在,随着数据的增长,标记开始变得狭窄,我甚至在同一建筑物上有两个地方的情况。我知道这个问题之前已经被问了很多,我的是一个简单的时间解决方法,我正在考虑实施。如果两个地方足够接近(在彼此的顶部!),标准信息窗口会弹出一个ListView,每行设计得像标准气泡(图像,标题,moreInfoButton)。

我的问题是:关于如何创建新的bonuspack_bubble.xml布局文件的一些想法或建议。

1 个答案:

答案 0 :(得分:4)

我不知道这是否会对你有所帮助。 我需要为我的项目创建一个CustomInfoBubble。我做的是,扩展InfoWindow默认类,并传递给我自定义的气泡布局。像这样的东西: http://mobiledevstories.wordpress.com/2014/03/01/osmdroid-bonus-pack-markers-with-clickable-infowindows/

我的Java类MapCustomInfoBubble看起来像这样:

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
            }
        });
    }

}

在我的XML文件中,我有:

<?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>

我的代码中的其他地方,然后使用:

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();

在我的代码中,我使用button的标题在Marker上设置了文字。 Marker是我点击的项目。如果您想在同一InfoWindowListView内)中放置有关更多标记的信息,我想您需要事先了解信息的内容。

我相信,你可以在onOpen()内放置你想要的任何代码,但是,我不确定这是不是一个好习惯。您可以尝试创建自定义Constructor并将逻辑放在那里。它应该工作。 您需要将Resource Id(布局)和mapView传递给超级构造函数,因此它返回一个有效的mView对象。