是否可以使在 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/