在Google地图中自动添加多个地理位置和标记

时间:2011-08-18 20:49:34

标签: android google-maps google-maps-markers

是否可以更加“自动”在地图中添加地理位置?我的意思是,如果我们在地图中添加了许多点(超过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);

是否有方法将它们全部存入表中,然后编译器逐个添加它们?

2 个答案:

答案 0 :(得分:10)

您希望将它们存储在SQLite数据库或其他形式的数据存储中,然后您可以将它们拉入并使用ItemizedOverlay将它们放在地图上,请参阅 Google Map View < / em>(教程)。

从数据库光标创建数组

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对象只是一个具有纬度和经度的自定义对象。

希望这适合你。

答案 1 :(得分:2)

您可以将它们存储在SQLite数据库中,并根据需要提取它们