Guys I have to show markers on google maps with zoom.When user zoom in or zoom out markers will be visible covering the map area which is on the screen.How can I do this using Android google map?
Asked
Active
Viewed 2,591 times
1
-
1check this http://stackoverflow.com/questions/2013443/on-zoom-event-for-google-maps-on-android – Manoj Aug 08 '14 at 07:47
-
1So you want to add markers whenever you zoom in or out? – Evan Aug 08 '14 at 07:47
-
@Evan yes I want this. – Robin Royal Aug 08 '14 at 07:51
2 Answers
6
Okay so you can use a CameraChangeListener:
private HashMap<Integer, Marker> courseMarkers = new HashMap<Integer, Marker>();
//all your visible markers
ArrayList<Item> yourMarkerList = new ArrayList<Item>();
//method to add all your markers with unique ids
addItemsToMap(); //first call to initially show the markers you want
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
private float currentZoom = -1; //keep track of your current zoom level
@Override
public void onCameraChange(CameraPosition camera) {
if (camera.zoom != currentZoom){
currentZoom = camera.zoom;
//here you will then check your markers
addItemsToMap(yourMarkerList);
}
}
});
You will need a class Item that has a variable for a unique int id and a MarkerOptions
private void addItemsToMap(List<Item> items)
{
if(this.mMap != null)
{
//This is the current user-viewable region of the map
LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;
//Loop through all the items that are available to be placed on the map
for(Item m : item)
{
//If the item is within the the bounds of the screen
if(bounds.contains(item.getMarker().getPosition()))
{
//If the item isn't already being displayed
if(!courseMarkers.containsKey(item.getId()))
{
//Add the Marker to the Map and keep track of it with the HashMap
//getMarkerForItem just returns a MarkerOptions object
this.courseMarkers.put(item.getId(), this.googleMap.addMarker(item.getMarker())); //getmarkerforitem
}
}
//If the marker is off screen
else
{
//If the course was previously on screen
if(courseMarkers.containsKey(item.getId()))
{
//1. Remove the Marker from the GoogleMap
courseMarkers.get(item.getId()).remove();
//2. Remove the reference to the Marker from the HashMap
courseMarkers.remove(item.getId());
}
}
}
}
}
This should do it

Evan
- 287
- 4
- 14
-
I have already done this in my code.Now I need help in markers I have a list of points which I need to show/hide on zoom in and zoom out.how can I do that? – Robin Royal Aug 08 '14 at 07:56
-
1
-
-
-
1Look at the top bit of code, it's a HashMap, a data structure that links a key to an Object – Evan Aug 08 '14 at 09:34
-
-
@Even Thank You so much for ur support.Your answer was great but there is little problem which i need to resolve as soon as i done I'll accept this answer. – Robin Royal Aug 08 '14 at 11:40
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58962/discussion-between-robin-royal-and-evan). – Robin Royal Aug 08 '14 at 13:14
0
private void pointToPosition(LatLng position) {
//Build camera position
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position)
.zoom(15).build();
//Zoom in and animate the camera.
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

Partrick_Android
- 1
- 2