I am new to clustering marker in android. I implemented ClusterItem and added all other marker[Hotels] and user maker in ClusterManager. But problem is that when i don't add user marker to ClusterManager it does not cluster other maker too.
What i want is all other marker should be clustered expect user maker. Please help me how can i achieve this.
This is how i am creating clustermanager
clusterManager = new ClusterManager<ClusterItem>(this, mMap);
mMap.setOnCameraIdleListener(clusterManager);
mMap.setOnMarkerClickListener(clusterManager);
and i am adding clusterItem just like we do
clusterManager.addItem(new ClusterItem(hotelsLocation));
But problem is that when i don't add user marker to ClusterManager it does not cluster other maker too.
Can you make it more clear ? If you want add or remove a cluster item on the fly, use
mClusterManager.addItem(item);
// mClusterManager.removeItem(item);
mClusterManager.cluster();
the cluster() method here means, just an item added/removed please calculate clusters again.
Init the clusterManager like this:
clusterManager = new ClusterManager<>(getActivity(), googleMap);
clusterManager.setRenderer(new OurClusterRenderer(getActivity(), googleMap, clusterManager));
googleMap.setOnCameraChangeListener(clusterManager);
googleMap.setOnMarkerClickListener(clusterManager);
googleMap.setOnInfoWindowClickListener(clusterManager);
googleMap.setInfoWindowAdapter(clusterManager.getMarkerManager());
googleMap.setOnInfoWindowClickListener(clusterManager);
Then add your own marks (Hotels only) and call .cluster():
clusterManager.clearItems();
for (HotelModel hotelModel : hotelModelNearMeList) {
if (hotelModel.getPosition() != null) {
clusterManager.addItem(hotelModel);
}
}
clusterManager.cluster();
Finally, if you want to add a User Marker:
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(location.getLatitude(),location.getLongitude()));
markerOptions.anchor(0.5f, 1);
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_mylocation));
googleMap.addMarker(markerOptions);
If you follow this steps, your clustering will be done even if you don't add any UserMarker. Hope it helps.
Related
I have a few markers added on a map. I'm using a model class to hold data, so I'm adding the markers using the following code:
LatLng latLng = new LatLng(model.getLat(), model.getLng());
MarkerOptions marker = new MarkerOptions().position(latLng).title(model.getName());
googleMap.addMarker(marker);
I'm also using a method to move the camera to a specific position on user click. My method looks like this:
private void moveCamera(Model model) {
moveCamera(new LatLng(model.getLat(), model.getLng()), DEFAULT_ZOOM);
}
So when the user clicks on the name of a location which is in a list, I'm moving the camera over that location. How can I automatically open the title of that marker, when the camera arrives above that location?
Thanks in advance!
You have this code to create your MarkerOptions:
LatLng latLng = new LatLng(model.getLat(), model.getLng());
MarkerOptions markerOption = new MarkerOptions().position(latLng).title(model.getName());
When you add the Marker to your map just keep a reference to the object:
Marker marker = googleMap.addMarker(markerOption);
Now you can show the info window:
marker.showInfoWindow();
I'm currently developing an android application that would allow users to draw Polylines with Markers on the map. Right now, I would like to implement a feature whereby the polyline will be draggable whenever the marker is being dragged and update the Polyline when the onMarkerDragEnd() method is being called. Does anyone know how I can achieve this? Below is a snippet of my codes. Thanks!
googleMap.setOnMapClickListener(new OnMapClickListener(){
#Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
if(drawMode == true && arrayPoints.isEmpty()){
MarkerOptions marker=new MarkerOptions();
marker.position(point);
googleMap.addMarker(marker).setDraggable(true);
arrayPoints.add(point);
marker.draggable(true);
}
else if(drawMode == true){
Log.e("","IN SECOND");
MarkerOptions marker=new MarkerOptions();
marker.position(point);
googleMap.addMarker(marker).setDraggable(true);
arrayPoints.add(point);
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.BLUE);
polylineOptions.width(5);
polylineOptions.addAll(arrayPoints);
Polyline drawRoute = googleMap.addPolyline(polylineOptions);
}
}
});
At first make Polyline drawRoute a field instead of a local variable.
Then you can update the polyline inside onMarkerDragEnd by calling drawRoute.setPoints(arrayPoints).
Then you need in addition a Java-Map, which keeps track of which marker was responsible for which point in the array. The map would have the marker-ID as key and the array-index as value. (You get the marker ID from the marker which is returned by map.addMarker)
When a marker is dragged, you can find out the index of the corresponding Point in arrayPoints using the marker-ID and said Java-Map. With that, you can exchange the point in the array and call drawRoute.setPoints again.
I'm adding my position to map using MarkerOptions as follows:
userMarker = new MarkerOptions().position(latLng).title("Current Location");
mMap.addMarker(userMarker);
How could it remove it from the map?
Back in the old days, Marker Class had a remove() method, but MarkerOptions has nothing similar... I also checked mMap (which is a GoogleMap), but no luck... :(
The addMarker() method returns a Marker object that you can work with:
userMarker = new MarkerOptions().position(latLng).title("Current Location");
Marker myMarker = mMap.addMarker(userMarker);
And then remove your Marker doing
myMarker.remove();
It appears that MarkerOptions class has a few methods that could help you like:
public MarkerOptions visible(boolean visible);
This basically sets visibility status of your marker.
MarkerOptions updatedMarker = userMarker.visible(false);
It also returns the MarkerOptions object with its new status updated!
You can find more information on this by clicking here.
I hope this helps you!
The Marker class has a remove method.
Simply call: marker.remove()
this code is remove with click infoWindow
// Setting click event handler for InfoWIndow
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// Remove the marker
marker.remove();
}
});
but, How remove a single marker by Id without click infoWindow ? i will remove by Button View
Whenever you add the marker to map don't forget to keep its record, like adding it to Map or ArrayList.
your_google_map_obj.addMarker(new MarkerOptions()) //this adds Marker on Google Map, you
should know it always returns Marker object so that you can use it later especially for
removal
so Marker marker=your_google_map_obj.addMarker(new MarkerOptions()) add this marker object to list or map markerArraylist.add(marker); then easily you can extract marker from list by Marker marker=markerArraylist.get(index); and then call marker.remove();
Other way to do it
After adding the marker it is possible to obtain its reference:
Marker marker = map.addMarker(..);
Marker class has remove method, check this documentation
Can anyone help me in the following task:
I want to add a marker in google map in android.
The functionality has to be like this that a pop up window have to be shown to add the touched location as a marker.
I was referring the below tutorial in that they add the marker through hard coding.
http://developer.android.com/resources/tutorials/views/hello-mapview.html
I want it that to be done using onclck on the map.
I used Google Maps API v2 and the solution is given below:
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
MarkerOptions marker = new MarkerOptions()
.position(new LatLng(point.latitude, point.longitude))
.title("New Marker");
googleMap.addMarker(marker);
System.out.println(point.latitude + "---" + point.longitude);
}
});
In MapView you must use onTouch instead of onClick. The motionEvent that this event fires, has the touch coordinates so with the getProjection() method from MapView you can convert the touch coordinates into lat and long to put the Overlay (Marker) on the map.