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"));
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 have a MapActivity in my Android application. I have added marker to a particular location as
mMap.addMarker(new MarkerOptions()
.title(values[1])
.position(stepsInThePath.get(Integer.valueOf(values[0])).startLocation));
But the title appear only when the marker is clicked.
What I want?
I want to show the title on the marker as soon as the marker is created (i.e, without clicking). The title should disappear after... say 5 seconds.
Any kind of help will be greatly appreciated.
Thanks!
Try this one,
final Marker marker = mMap.addMarker(new MarkerOptions()
.title(values[1])
.position(stepsInThePath.get(Integer.valueOf(values[0])).startLocation));
marker.showInfoWindow();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (marker!=null)
{
//If you only hide marker title
marker.hideInfoWindow();
//If you want to remove marker
marker.remove();
}
}
}, 5000);//Millisecond for remove marker
Hi,
1. Please find the route and map icon in the highlighted area.
2. These are shown on touch of the my location blue circle .
3. want to show those icons by default, without the user touching, as soon as the map loads.
Below is the code :
protected void loadMap(GoogleMap googleMap, String latlng) {
if (googleMap != null) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
String[] latlngAry = latlng.split(",");
double lat = Double.parseDouble(latlngAry[0]);
double lng = Double.parseDouble(latlngAry[1]);
LatLng latlong = new LatLng(lat, lng);
BitmapDescriptor defaultMarker =
BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(latlong)
.title("My Location")
.icon(defaultMarker));
marker.showInfoWindow();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 18)); }
The overlay that appears when a marker is clicked, is created and destroyed on-the-spot implicitly. You can't manually show that (yet).
If you must have this functionality, you can create an overlay over your map with 2 ImageViews, and call appropriate intents when they're clicked:
// Directions
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
"http://maps.google.com/maps?saddr=51.5, 0.125&daddr=51.5, 0.15"));
startActivity(intent);
// Default google map
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
"http://maps.google.com/maps?q=loc:51.5, 0.125"));
startActivity(intent);
Note: you need to change the coordinates based on Marker's getPosition() and the user's location.
Now to hide the default overlay, all you need to do is return true in the OnMarkerClickListener. Although you loose the ability to show InfoWindows and center camera on the marker, you can imitate that simply enough:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
return true;
}
});
How to add a pin(not a marker)to the google map in android?
I know how to add a marker, something like this:
map.addMarker(new MarkerOptions()
.position(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()))
.title(getString(R.string.start_location_marker)));
But I want a pin, not a marker.
A pin looks like this:
pin
A marker looks like this:
marker
set your pin image to marker it is called custom marker
#Override
public void onMapReady(GoogleMap googleMap) {
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.pin);
LatLng latLng = new LatLng(21.4225, 39.8262);
googleMap.addMarker(new MarkerOptions().position(latLng).title("Mecca").icon(icon)).showInfoWindow();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}
In my App users use a map to mark their location. When new users want to create their location I would like to prevent them from putting their marker to close to already existing markers, if possible to specify mandatory distance between markers for example 10 or 20 meters.
Thank you.
This is actually very simple, all you need to do is check if the point that the user clicks on is further than your required distance before removing the previous Marker and adding the new one.
You can use the Location.distanceBetween() method to do the distance check.
First, create your Marker reference as an instance variable of the Activity:
Marker marker;
Then, in your OnMapClickListener, add the logic to only move the Marker that the user chooses as the current location if the distance is greater than your defined minimum distance, 20 meters in this example:
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
if (marker != null) {
LatLng markerPoint = marker.getPosition();
float[] distance = new float[2];
Location.distanceBetween(point.latitude, point.longitude,
markerPoint.latitude, markerPoint.longitude, distance);
//check if new position is at least 20 meters away from previous selection
if( distance[0] > 20 ){
//remove previous Marker
marker.remove();
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("My Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
else{
//Alert the user to select a location further away from the one already selected
Toast.makeText(MainActivity.this, "Please select a different location.", Toast.LENGTH_SHORT).show();
}
}
else {
//No previous selection, place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("My Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
}
});