Add multiple geopoints and markers automatically i

2020-05-29 12:31发布

问题:

Is it possible to make the addition of a geopoint in a map more "automatic"? I mean, if we have many points to add in the map (more than 100), we don't have to add them one by one like that:

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);

Is there a method to stock them all in a table and then the compiler adds them one by one?

回答1:

You'll want to store them in either a SQLite database or some other form of data storage, and then you can pull them in and place them on the map with an ItemizedOverlay, see Google Map View (a tutorial).

Create your array from a database cursor

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 is my own extended ItemizedOverlay (I needed custom functionality). The catchList object is just a custom object that has latitude and longitude.

Hopefully this works for you.



回答2:

You could store them in a SQLite databse, and pull them as needed