Adding multiple markers in Google Maps API v2 Android

北城余情 提交于 2019-11-27 18:24:53
nbaroz
ArrayList<MarkerData> markersArray = new ArrayList<MarkerData>();

for(int i = 0 ; i < markersArray.size() ; i++) {

    createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(), markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());
}


protected Marker createMarker(double latitude, double longitude, String title, String snippet, int iconResID) {

    return googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .anchor(0.5f, 0.5f)
            .title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.fromResource(iconResID)));
}

Use MarkerOptions

private GoogleMap googleMap;
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> latlngs = new ArrayList<>();

You can add to the list of latlngs by,

 latlngs.add(new LatLng(12.334343, 33.43434)); //some latitude and logitude value

And then, use for loop to set them on the map.

 for (LatLng point : latlngs) {
     options.position(point);
     options.title("someTitle");
     options.snippet("someDesc");
     googleMap.addMarker(options);
 }

So, if you get coordinates from txt file you could read them like this:

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt"), "UTF-8")); 

    // do reading, usually loop until end of file reading 
    String mLine = reader.readLine();
    while (mLine != null) {
       //process line
       ...
       mLine = reader.readLine(); 
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

if your txt file looks like this

23.45 43.23
23.41 43.65
.
.
.

Than you could modify string to LatLng objects:

String[] coord = mLine.split("\\r?\\n");
ArrayList<LatLng> coordinates = new ArrayList<LatLng>;

for(int i = 0; i <  coord.lenght(); ++i){
    String[] latlng =   coord.split(" ");
    coordinates.add(new LatLng(latlng[0], latlng[1]);
}

And than:

for(LatLng cor : coordinates){
    map.addMarker(new MarkerOptions()
       .position(cor.getLat(), cor.getLng())
       .title("Hello"));
}

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<Marker, MyMarkerData> mDataMap = new HashMap<>();

        public void drawMarkers(ArrayList<MyMarkerData> 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);
                }
            }
A-Droid Tech

Yes , you can use the ArrayList for storing all the marker in that list and after that use the for-loop for adding markers on map.

For example:

googleMap.clear();
Now get all the marker in the Markers
//seachModelsList is the list of all markers
Marker[] allMarkers = new Marker[seachModelsList.size()];

for (int i = 0; i < seachModelsList.size(); i++)
{
    LatLng latLng = new LatLng(seachModelsList.get(i).getCoordinates()[1], seachModelsList.get(i)
            .getCoordinates()[0]);
    if (googleMap != null) {
        googleMap.setOnMarkerClickListener(this);
        allMarkers[i] = googleMap.addMarker(new MarkerOptions().position(latLng);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!