android - 在 Google map 中自动添加多个地理点和标记

标签 android google-maps google-maps-markers

是否可以使在 map 中添加地理点更加“自动”?我的意思是,如果我们有很多点要添加到 map 中(超过 100 个),我们不必像那样一个一个地添加它们:

GeoPoint point2 = new GeoPoint(microdegrees(36.86774),microdegrees(10.305302));
GeoPoint point3 = new GeoPoint(microdegrees(36.87154),microdegrees(10.341815));
GeoPoint point4 = new GeoPoint(microdegrees(36.876093),microdegrees(10.325716));  

pinOverlay.addPoint(point2);
pinOverlay.addPoint(point3);
pinOverlay.addPoint(point4);

有没有一种方法可以把它们都存到一个表里,然后编译器一个一个地添加?

最佳答案

您需要将它们存储在 SQLite 数据库或某种其他形式的数据存储中,然后您可以将它们拉入并使用 ItemizedOverlay 将它们放置在 map 上,参见 Google Map View (教程)。

从数据库游标创建数组

Cursor cursor =  mDbHelper.getItems();
cursor.moveToFirst();

List<CatchItem> catchList = new ArrayList<CatchItem>();

if (cursor != null && cursor.getCount() > 0) {
    for (int i = 0; i < cursor.getCount(); i++) {
        CatchItem item = new CatchItem();

        item.Latitude = cursor.getDouble(cursor.getColumnIndex("latitude"));
        item.Longitude = cursor.getDouble(cursor.getColumnIndex("longitude"));

        catchList.add(item);
        cursor.moveToNext();
    }
}

ItemizedOverlay

List<Overlay> overlays = mMaps.getOverlays();
overlays.clear();
CatchesItemizedOverlay catchOverlays = new CatchesItemizedOverlay(getResources().getDrawable(R.drawable.map_markeroverlay_blue72), this);

for (int i = 0; i < catchList.size(); i++) {
    double lat = catchList.get(i).Latitude;
    double lng = catchList.get(i).Longitude;

    GeoPoint geopoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
    catchOverlays.addOverlay(new CatchOverlayItem(this, geopoint, catchList.get(i)));
}

overlays.add(catchOverlays);

CatchesItemizedOverlay 是我自己的扩展 ItemizedOverlay(我需要自定义功能)。 catchList 对象只是一个具有纬度和经度的自定义对象。

希望这对你有用。

关于android - 在 Google map 中自动添加多个地理点和标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7113980/

相关文章:

android - 如果您有设备 key ,是否可以访问另一个应用程序数据库?

javascript - 谷歌地图错误?显示的标记根据缩放级别而变化

javascript - 动画符号 - Google map API

javascript - map 略微移动或单击后才会显示标记

c# - 从数据库加载 map 范围内的坐标

android - TabHost 不适用于 view pager android

android - 如何在 mediaplayer android 上播放 mp3+g

Android:如何在以前的 api 级别上支持 Material 设计 (api 21) 功能?

ios - [GMSCachedTile setVersionID :]: unrecognized selector

java - 在android中的 map 上添加五个标记