I want to add multiple markers in my map, but I don't know the way.
At the moment, im 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?
Thanks.
Use
MarkerOptions
You can add to the list of latlngs by,
And then, use for loop to set them on the map.
So, if you get coordinates from txt file you could read them like this:
if your txt file looks like this
Than you could modify string to LatLng objects:
And than:
It depends on the source of your data. The better way is to make your custom object to store data. For example:
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:
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: