以下是一个示例代码,使用 osmdroid 库批量添加多种标记和弹窗,样式不同,不同的标记放入相同的图层进行管理:

// 创建标记图层
ItemizedIconOverlay<OverlayItem> markerOverlay = new ItemizedIconOverlay<OverlayItem>(new ArrayList<OverlayItem>(), null, new DefaultResourceProxyImpl(context));

// 创建弹窗图层
ItemizedOverlayWithFocus<OverlayItem> popupOverlay = new ItemizedOverlayWithFocus<OverlayItem>(new ArrayList<OverlayItem>(), null, new DefaultResourceProxyImpl(context));

// 创建标记和弹窗
OverlayItem marker1 = new OverlayItem('Title 1', 'Description 1', new GeoPoint(0, 0));
marker1.setMarker(context.getResources().getDrawable(R.drawable.marker1));
markerOverlay.addItem(marker1);

OverlayItem popup1 = new OverlayItem('Title 1', 'Description 1', new GeoPoint(0, 0));
popup1.setMarker(context.getResources().getDrawable(R.drawable.popup1));
popupOverlay.addItem(popup1);

OverlayItem marker2 = new OverlayItem('Title 2', 'Description 2', new GeoPoint(0, 0));
marker2.setMarker(context.getResources().getDrawable(R.drawable.marker2));
markerOverlay.addItem(marker2);

OverlayItem popup2 = new OverlayItem('Title 2', 'Description 2', new GeoPoint(0, 0));
popup2.setMarker(context.getResources().getDrawable(R.drawable.popup2));
popupOverlay.addItem(popup2);

// 将标记和弹窗图层添加到地图中
map.getOverlays().add(markerOverlay);
map.getOverlays().add(popupOverlay);

// 注册弹窗点击事件
popupOverlay.setOnFocusChangeListener(new ItemizedOverlayWithFocus.OnFocusChangeListener<OverlayItem>() {
    @Override
    public void onFocusChanged(ItemizedOverlayWithFocus<OverlayItem> overlay, OverlayItem newFocus) {
        // 显示弹窗
        if (newFocus != null) {
            // 创建弹窗布局
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popupView = inflater.inflate(R.layout.popup_layout, null);

            // 设置弹窗内容
            TextView titleText = (TextView) popupView.findViewById(R.id.popup_title);
            TextView descText = (TextView) popupView.findViewById(R.id.popup_description);
            titleText.setText(newFocus.getTitle());
            descText.setText(newFocus.getSnippet());

            // 显示弹窗
            PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.showAtLocation(map, Gravity.CENTER, 0, 0);
        }
    }
});

// 优化:使用缓存的标记和弹窗
// 在创建标记和弹窗时,可以将它们添加到缓存中
Map<String, Drawable> markerCache = new HashMap<String, Drawable>();
Map<String, View> popupCache = new HashMap<String, View>();

OverlayItem marker1 = new OverlayItem('Title 1', 'Description 1', new GeoPoint(0, 0));
if (markerCache.containsKey('marker1')) {
    marker1.setMarker(markerCache.get('marker1'));
} else {
    Drawable markerDrawable = context.getResources().getDrawable(R.drawable.marker1);
    marker1.setMarker(markerDrawable);
    markerCache.put('marker1', markerDrawable);
}
markerOverlay.addItem(marker1);

OverlayItem popup1 = new OverlayItem('Title 1', 'Description 1', new GeoPoint(0, 0));
if (popupCache.containsKey('popup1')) {
    popup1.setMarker(popupCache.get('popup1'));
} else {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.popup_layout, null);
    // 设置弹窗内容
    TextView titleText = (TextView) popupView.findViewById(R.id.popup_title);
    TextView descText = (TextView) popupView.findViewById(R.id.popup_description);
    titleText.setText('Title 1');
    descText.setText('Description 1');
    popupView.setBackgroundResource(R.drawable.popup1);
    popup1.setMarker(new BitmapDrawable(getResources(), createBitmapFromView(popupView)));
    popupCache.put('popup1', popup1.getMarker());
}
popupOverlay.addItem(popup1);

// 可以自定义一个方法,将View转换为Bitmap
private Bitmap createBitmapFromView(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

优化说明:

  • 使用缓存来存储标记和弹窗的 Drawable 对象,避免重复创建 Drawable,提高性能。
  • 使用 BitmapDrawable 将 View 转换为 Bitmap,然后将其设置为标记,这样可以避免使用自定义 Marker 类。
  • 可以根据需要自定义标记和弹窗的样式,例如使用不同的图标、颜色和文本。
  • 可以在代码中添加注释,以提高代码的可读性和可维护性。

注意:

  • R.drawable.marker1 和 R.drawable.popup1 应替换为实际的资源文件名称。
  • R.layout.popup_layout 应替换为实际的布局文件名称。
  • R.id.popup_title 和 R.id.popup_description 应替换为实际的视图 ID。

本文提供了一种基本示例,您可以根据自己的需求进行扩展和修改。

以下是一些额外的提示:

  • 使用线程池来加载标记和弹窗,避免阻塞主线程。
  • 使用异步任务来加载地图数据,避免阻塞用户界面。
  • 使用地图缓存来提高地图加载速度。
  • 使用地图压缩技术来减小地图文件大小。
  • 使用地图离线数据,以便在没有网络连接的情况下使用地图。

通过这些优化和技巧,您可以创建更加高效、易用、功能强大的 Android 地图应用程序。

Android Osmdroid 批量添加多种标记和弹窗:样式不同,图层管理,优化代码

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

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