gpt4 book ai didi

java - 设置标注图像 mapbox android

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:39 25 4
gpt4 key购买 nike

在 iOS 中,您可以通过调用以下方式轻松地为您的标记设置标注:

[marker setCanShowCallout:YES];
[marker setRightCalloutAccessoryView:YOUR_BUTTON];

但是我找不到 Mapbox Android SDK 的这个功能。我现在有一个监听器可以检测 calloutview 上的触摸,但我如何设置 callout 图像/按钮?

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

最佳答案

您必须自己构建此功能,但实际上通过定义自定义标记和工具提示布局很容易实现。

首先定义您的工具提示布局。显然必须是 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);
}
}

这不应更改任何其他功能,因此您拥有的代码仍然有效。只需将标记更改为自定义类名,您就应该设置好了。

编辑:这是实际使用我的 map 设置 CustomMarker 和 InfoWindow 的代码。你可以看到我在点击时启动了另一个 Activity ,并且我已经为标记设置了一个自定义图标,但这些东西不是必需的:

        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。

关于java - 设置标注图像 mapbox android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24013082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com