27

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.addMarker(new MarkerOptions()
                .position(new LatLng(38.609556, -1.139637))
                .anchor(0.5f, 0.5f)
                .title("Title1")
                .snippet("Snippet1")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.logo1)));


Marker m2 = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(40.4272414,-3.7020037))
                .anchor(0.5f, 0.5f)
                .title("Title2")
                .snippet("Snippet2")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.logo2)));

Marker m3 = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(43.2568193,-2.9225534))
                .anchor(0.5f, 0.5f)
                .title("Title3")
                .snippet("Snippet3")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.logo3)));

But the problem comes when I want to add 300 markers in my map. And doing it one by one is very annoying.

Is there any way to read markers from array or anything?

Another question: could I read markers from external file, so I can add or update markers without touching app code?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tárraga
  • 461
  • 1
  • 6
  • 13
  • 2
    You need to store your `LatLag` in one arrayList and use for loop to add multiple markers. – Piyush Jun 01 '15 at 09:25

5 Answers5

69
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)));
}
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
nbaroz
  • 1,795
  • 13
  • 14
20

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);
 }
siriscac
  • 1,699
  • 12
  • 21
2

In Kotlin you can do as :-

Let you have list of markerData as markerList

val markerList = ArrayList<MarkerData>()

and then you can iterate list through forEach loop and add Marker in GoogleMap as :-

 markerList.forEach{ markerData ->
     googleMap.addMarker(MarkerOptions()
    .position(LatLng(markerData.latitutde, markerData.longitude))
    .anchor(0.5f, 0.5f)
    .title(markerData.title)
    .snippet(markerData.snippet)
    .icon(BitmapDescriptorFactory.fromResource(markerData.iconResID)))
   }

Suppose your MarkerData is :-

class MarkerData(val latitutde : Double, val longitude : Double, val title : String, val snippets: String, @DrawableRes val iconResID: Int)

And you are adding MarkerData as -:

MarkerData(
            35.61049,
            139.63007,
            "Tokyo",
            "hello Tokyo",
            R.drawable.ic_icon_user_review
        )

then you have to create a custom method for vector assets icon like this :-

 private fun bitmapDescriptorFromVector(context: Context, vectorResId: Int): BitmapDescriptor? {
    return ContextCompat.getDrawable(context, vectorResId)?.run {
        setBounds(0, 0, intrinsicWidth, intrinsicHeight)
        val bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888)
        draw(Canvas(bitmap))
        BitmapDescriptorFactory.fromBitmap(bitmap)
    }
}

and then for Marker your method would be as :

 markerList.forEach{ markerData ->
     googleMap.addMarker(MarkerOptions()
    .position(LatLng(markerData.latitutde, markerData.longitude))
    .anchor(0.5f, 0.5f)
    .title(markerData.title)
    .snippet(markerData.snippet)
    .icon(bitmapDescriptorFromVector(context!!, R.drawable.ic_icon_marker)))
   }
Alok Mishra
  • 1,904
  • 1
  • 17
  • 18
1

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

    }
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
A-Droid Tech
  • 2,301
  • 24
  • 33
0

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);
                }
            }
Georgiy Shur
  • 748
  • 1
  • 8
  • 19
  • So, where should I add the data? – Tárraga Jun 01 '15 at 10:06
  • It depends on you, but definitely before calling draw method. If you're using external file, try to find its conversion to Java object – Georgiy Shur Jun 01 '15 at 10:19
  • I should ask too how to add data by this way, or if you can give me an example. – Tárraga Jun 01 '15 at 10:25
  • Something like: "38.609556, -1.139637, Title1, Snippet1, icon1" – Tárraga Jun 01 '15 at 10:43
  • Well you should either write your own parser, that will read your text file and write this information to Java objects, or consider using JSON or some other popular format, that has already its parsing mechanisms. But anyway it is a whole new question, I think there is enough information about this here on stackoverflow, try to google it. – Georgiy Shur Jun 01 '15 at 11:03