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()
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();
How can I clear the recent markers or declare a new marker without using the .addMarker or new Tag? (or any way to do this, if you have an idea)
I use the codes below to add a marker(the one in comment) and it works but since it's in ".addMarker(new Marker...) tag, it creates a new marker every time I click my button. I tried to make a declared marker, and set the marker position, but I think it just doesn't work that way.
Concept: I'm getting my current location onClick, but it creates a new marker instead of re-positioning the last marker I have. T.I.A.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
x = Double.valueOf(locX);
y = Double.valueOf(locY);
LatLng loc = new LatLng(x, y);
//mMap.addMarker(new MarkerOptions().position(loc).draggable(true).title("Building Location"));
marker.setPosition(loc);
marker.isDraggable();
marker.setTitle("Building Location");
mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
you can clear old marker before setting a new one like this -
mMap.clear(); //add this , it will clear old markers. if any
//then set new marker.
mMap.addMarker(new MarkerOptions().position(YOUR_NEW_LOCATION).draggable(true).title("Building Location"));
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 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.
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.