要批量创建标记对象并刷新位置和自定义弹窗信息,您可以按照以下步骤进行操作:

  1. 创建一个类来表示标记对象。该类应该包含标记的位置、标题、描述等信息。例如:
public class MarkerObject {
    private GeoPoint location;
    private String title;
    private String description;

    public MarkerObject(GeoPoint location, String title, String description) {
        this.location = location;
        this.title = title;
        this.description = description;
    }

    public GeoPoint getLocation() {
        return location;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}
  1. 在您的活动或片段中,创建一个列表来保存所有标记对象。例如:
List<MarkerObject> markerList = new ArrayList<>();
  1. 使用循环遍历列表,并为每个标记对象创建一个Marker对象。您可以使用Marker的构造函数来设置位置和图标等属性。例如:
for (MarkerObject markerObject : markerList) {
    Marker marker = new Marker(mapView);
    marker.setPosition(markerObject.getLocation());
    marker.setIcon(ContextCompat.getDrawable(this, R.drawable.marker_icon));
    marker.setTitle(markerObject.getTitle());
    marker.setSnippet(markerObject.getDescription());
    mapView.getOverlays().add(marker);
}
  1. 要刷新标记的位置,您可以更新Marker对象的位置属性并调用mapView.invalidate()方法。例如:
for (MarkerObject markerObject : markerList) {
    Marker marker = getMarkerForLocation(markerObject.getLocation());
    if (marker != null) {
        marker.setPosition(markerObject.getLocation());
    }
}

mapView.invalidate();

在上面的代码中,getMarkerForLocation()是一个自定义方法,用于从mapView.getOverlays()中获取具有指定位置的Marker对象。

  1. 要自定义标记的弹窗信息,您可以实现InfoWindow接口,并将其设置为Marker对象的弹窗。例如:
public class CustomInfoWindow implements InfoWindow {
    private Context context;
    
    public CustomInfoWindow(Context context) {
        this.context = context;
    }

    @Override
    public void onOpen(Object item) {
        Marker marker = (Marker) item;
        // 在这里设置自定义弹窗的布局和内容
        View view = LayoutInflater.from(context).inflate(R.layout.custom_info_window, null);
        TextView titleTextView = view.findViewById(R.id.titleTextView);
        TextView descriptionTextView = view.findViewById(R.id.descriptionTextView);
        titleTextView.setText(marker.getTitle());
        descriptionTextView.setText(marker.getSnippet());

        // 显示弹窗
        InfoWindow.openMarkerInfoWindow(view, marker);
    }

    @Override
    public void onClose() {
        // 关闭弹窗
        InfoWindow.closeAllInfoWindowsOn(mapView);
    }
}

在上面的代码中,custom_info_window是一个自定义布局文件,用于显示标记的标题和描述。

  1. 在创建Marker对象时,将CustomInfoWindow对象设置为弹窗。例如:
for (MarkerObject markerObject : markerList) {
    Marker marker = new Marker(mapView);
    // 设置其他属性

    marker.setInfoWindow(new CustomInfoWindow(this));
    mapView.getOverlays().add(marker);
}

通过以上步骤,您可以批量创建标记对象并刷新位置和自定义弹窗信息。请注意,上述代码是基于osmdroid库的使用,您需要根据您的具体情况进行调整

osmdroid 批量创建标记对象 并批量刷新位置和自定义弹窗信息

原文地址: https://www.cveoy.top/t/topic/hHw5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录