Adding multiple markers in Google Maps API v2 Android

前端 未结 6 762
礼貌的吻别
礼貌的吻别 2020-11-30 02:23

I want to add multiple markers in my map, but I don\'t know the way.

At the moment, I\'m using this, and it works correctly:

Marker m1 = googleMap.ad         


        
6条回答
  •  失恋的感觉
    2020-11-30 02:25

    It depends on the source of your data. The better way is to make your custom object to store data. For example:

    public class MyMarkerData {
            LatLng latLng;
            String title;
            Bitmap bitmap;
    
            public LatLng getLatLng() {
                return latLng;
            }
    
            public void setLatLng(LatLng latLng) {
                this.latLng = latLng;
            }
    
            public String getTitle() {
                return title;
            }
    
            public void setTitle(String title) {
                this.title = title;
            }
    
            public Bitmap getBitmap() {
                return bitmap;
            }
    
            public void setBitmap(Bitmap bitmap) {
                this.bitmap = bitmap;
            }
        }
    

    Then, you can write some method to convert data from your external file to list of your custom data objects (but I think it is out of scope of this question).

    Then just pass this data to your marker-drawing method and loop through it. It's a good practice thought to save your markers in some arraylist or map(object, marker) too, then you can easily access it.

    Something like that:

        HashMap mDataMap = new HashMap<>();
    
            public void drawMarkers(ArrayList data) {
                    Marker m;
                    for (MyMarkerData object: data) {
                        m = googleMap.addMarker(new MarkerOptions()
                                .position(object.getLatLng())
                                .title(object.getTitle())
                                .icon(BitmapDescriptorFactory.fromBitmap(object.getBitmap()));
    
                        mDataMap.put(m, object);
                    }
                }
    

提交回复
热议问题