设置标注图像Mapbox Android
题
在iOS中,您可以通过调用:
轻松为您的标记设置标记[marker setCanShowCallout:YES];
[marker setRightCalloutAccessoryView:YOUR_BUTTON];
.
但我找不到mapbox android sdk的这个功能。我现在有一个倾听的听众,它检测到CalloutView上的触摸,但我如何设置标注图像/按钮?
Marker marker = new Marker(p.getTitle(), p.getCatagoryName(), new LatLng(p.getLatitude(), p.getLongitude()));
marker.setMarker(getResources().getDrawable(getResources().getIdentifier(string, "drawable", getActivity().getPackageName())));
mMapView.addMarker(marker);
InfoWindow toolTip = marker.getToolTip(mMapView);
View view = toolTip.getView();
// view.setBackgroundResource(R.drawable.callout_button); THIS DOES NOT WORK
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.e(TAG, "onTouch");
return true;
}
});
. 解决方案
ryansh要求我在自定义布局中包含具有IDS ToolTip_title和ToolTip_description的TextViews的优秀响应。我也必须添加第三个工具Tip_subdescriptiontextview。默认的Infowindow代码假定存在这些视图,如果没有,则会崩溃。对于更多控制,我扩展了Infowindow,overrode Onopen,并且能够使用我想要的任何布局我想要的工具提示。对于扩展标记类中的覆盖CreateToolTip,我自然地实例化并返回了我的扩展信息indow对象。
更新。以下是扩展标记&Infowindow以支持自定义工具提示的示例:
public class MapboxMarker extends Marker {
private MyInfoWindow mInfoWindow;
public class MyInfoWindow extends InfoWindow {
public MyInfoWindow(int layoutResId, MapView mapView) {
super(layoutResId, mapView);
}
public void onOpen(Marker overlayItem) {
//
// Set data on mInfoWindow.getView()
//
}
}
public MapboxMarker(MapView mv, LatLng aLatLng, MapController mapController){
super(mv, "Title", "Description", aLatLng);
}
@Override
protected InfoWindow createTooltip(MapView mv) {
mInfoWindow = new MyInfoWindow(R.layout.custom_tooltip_layout, mv);
return mInfoWindow;
}
}
. 其他提示
您必须自己构建此功能,但它实际上通过定义自定义标记和工具提示布局而易于实现。
首先定义工具提示布局。它必须是一个依赖于relativeLayout,以定位标注图像。将上下文设置为创建标记的任何活动。必须包含底部的TipView,因为它控制自定义工具提示的大小。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:id="@+id/parent_layout"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.XXX.MainActivity">
<TextView
android:id="@+id/tooltip_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="18sp"
android:maxEms="17"
android:layout_gravity="left"
android:layout_weight="1"
android:text="@string/toolTipTitle"/>
<TextView
android:id="@+id/tooltip_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="14sp"
android:maxEms="17"
android:text="@string/toolTipDescription"
android:layout_below="@+id/tooltip_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/marker_about"
android:src="@drawable/ic_action_about"
android:layout_toEndOf="@+id/tooltip_description"
android:layout_toRightOf="@+id/tooltip_description"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp" />
<com.mapbox.mapboxsdk.views.TipView
android:layout_width="132dp"
android:layout_height="10dp"/>
</RelativeLayout>
.
然后创建自定义标记类并覆盖createTooltip方法以返回具有新布局的信息,请执行以下操作:
public class CustomMarker extends Marker {
public CustomMarker(MapView mv, String aTitle, String aDescription, LatLng aLatLng){
super(mv, aTitle, aDescription, aLatLng);
}
@Override
protected InfoWindow createTooltip(MapView mv) {
return new InfoWindow(R.layout.custom_tooltip, mv);
}
}
.
这不应该更改任何其他功能,因此您仍将工作的代码。只需将标记更改为自定义类名,您应该设置。
编辑:以下是实际设置自定义市场的代码,并使用我的地图信息向上。您可以看到我正在点击的另一个活动启动另一个活动,我已经为标记设置了自定义图标,但是没有必要的东西:
CustomMarker m =
new CustomMarker(mv, report.issue, report.getFormattedDateString(), latLng);
m.setIcon(new Icon(this, Icon.Size.LARGE, "oil-well", "FF0000"));
//get the InfoWindow's view so that you can set a touch listener which will switch
//to the marker's detailed view when clicked
final InfoWindow infoWindow = m.getToolTip(mv);
View view = infoWindow.getView();
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//make sure to choose action down or up, otherwise the intent will launch twice
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
startActivity(intent);
//close the InfoWindow so it's not still open when coming back to the map
infoWindow.close();
}
return true;
}
});
mv.addMarker(m);
.
我也使用SDK 0.5.0-Snapshot。我不确定这是否适用于SDK的旧版本。