您需要自己构建此功能,但通过定义自定义标记和工具提示布局实际上很容易实现。
首先定义您的工具提示布局。显然它必须是一个RelativeLayout以定位呼出图片。将上下文设置为创建标记的任何Activity。底部的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>
然后创建一个自定义的Marker类,并重写createTooltip方法,返回一个具有新布局的InfoWindow:
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);
}
}
这不应该改变其他任何功能,因此您现有的代码仍将正常工作。只需将Marker更改为自定义类名称即可。
编辑:这是实际设置CustomMarker和InfoWindow与我的地图一起使用的代码。您可以看到我在单击时启动了另一个活动,并为标记设置了自定义图标,但这些内容并不是必需的:
CustomMarker m =
new CustomMarker(mv, report.issue, report.getFormattedDateString(), latLng);
m.setIcon(new Icon(this, Icon.Size.LARGE, "oil-well", "FF0000"));
final InfoWindow infoWindow = m.getToolTip(mv);
View view = infoWindow.getView();
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
startActivity(intent);
infoWindow.close();
}
return true;
}
});
mv.addMarker(m);
我也在使用SDK 0.5.0-SNAPSHOT版本。不确定这是否适用于旧版SDK。