我正在使用 osmdroid 和 osm Bonus Pack 开发 Android 离线 map 应用程序,并从外部存储加载图 block 和数据。现在,随着数据的增长,标记开始变得拥挤在一起,我什至遇到了同一建筑物上两个地方的情况。我知道这种问题以前已经被问过很多次了,我的问题是关于我正在考虑实现的一个简单的临时解决方法。如果两个地方足够接近(就在彼此的顶部!),标准信息窗口会作为 ListView 弹出,每一行设计得像标准气泡(图像、标题、更多信息按钮)。
我的问题是:关于如何创建新的bonuspack_bubble.xml布局文件的一些想法或建议。
最佳答案
不知道对你有没有帮助。
我需要为我的项目创建一个 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();
在我的代码中,我使用标记
的标题设置了按钮
上的文本。
Marker
是我点击的项目。如果您想将更多标记的信息放在同一个 InfoWindow
中(在 ListView
内),我认为您需要提前知道这些信息是什么。
我相信,您可以将任何您想要的代码放入 onOpen()
中,但是,我不太确定这是否是一个好的做法。您可以尝试创建自定义构造函数
并将逻辑放在那里。它应该有效。
您需要将Resource Id
(布局)和mapView
传递给 super 构造函数,以便它返回一个有效的mView
对象。
关于android - osmdroid 经典标记重叠的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747012/