Removing selected markers from google map - android

I am placing multiple markers on google map by using onMarkerClickListener, now I want to give user the option to remove any marker from the added markers. Can anyone suggest some way to do this.
my code for marker is
GoogleMap.OnMarkerClickListener listener = new
GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
AddGeofenceFragment dFragment = new AddGeofenceFragment();
// Show DialogFragment
dFragment.show(fm, "Dialog Fragment");
return true;
}
};
newmap.setOnMarkerClickListener(listener);
newmap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
// Animating to the touched position
newmap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
newmap.addMarker(markerOptions);
Log.d("ADDED LATITUDE",String.valueOf(latLng.latitude));
Log.d("ADDED LONGITUDE",String.valueOf(latLng.longitude));
Toast.makeText(getApplicationContext(),"Block area updated",Toast.LENGTH_LONG).show();
}
});

you can do this by implementing interface OnMarkerClickListener to the mapActivity. then you need to write your require code to delete the selected marker in the method:
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker)) {
//handle click here
marker.remove();
}
}

Related

Setting onMarkerClickListener on different markers

I want to set OnMarkerClickListener of different Markers. Here I want to print i variable value of loop whenever respective marker will get clicked. So I did by following way .. but it is not working , It display same last value 170 of loop on the Snackbar in every different marker click.. But I suppose to get 0,10,20,30....170 respectively in snackbar on different marker click.
Please help...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
//ON MARKER CLICK
final int finalI = i;
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Snackbar.make((View) findViewById(R.id.map),""+finalI,Snackbar.LENGTH_LONG).show();
return true;
}
});
}
}
Here is the marker which was created by loop
but I am getting same value to 170
To Resolve your problem you should have a marker array.
Try this:
First make your app to implement GoogleMap.OnMarkerClickListener
Then create a Marker array :
Marker[] marker = new Marker[20]; //change length of array according to you
then inside
onMapReady(){
mMap.setOnMarkerClickListener(this);
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
marker[i] = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
}
then finally
#Override
public boolean onMarkerClick(Marker marker) {
//you can get assests of the clicked marker
return false;
}
I found one way...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
//ON MARKER CLICK
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
for(int i=0;i<180;i=i+10) {
if (marker.getTitle().equals("Marker in Position" + i))
Snackbar.make((View) findViewById(R.id.map), "" + i, Snackbar.LENGTH_LONG).show();
}return true;
}
});
}

how to put particular marker in center from list of markers

i have ListView which showing the number of locations, when i click on any location the map is showing with marker of all the list of location but i want to show the selected location's marker in center and others are there area.
How to show the selected location in center and others are as it is
gMap is the object of GoogleMap
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
marker.showInfoWindow();
LatLng currentPoint = new LatLng(26.5886, 72.982);// pass here your selected marker coordinate(Latitude & Longitude) like lat
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentPoint, 7);// set here your zoom level
gMap.animateCamera(cameraUpdate);
return true;
}
});
for e.g.
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
marker.showInfoWindow();
YourModel myMarker = mMarkersHashMap.get(marker);// it is your model
LatLng currentPoint = new LatLng(Double.valueOf(myMarker.getLat()), Double.valueOf(myMarker.getLon()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentPoint, 7);
gMap.animateCamera(cameraUpdate);
return true;
}
});

on marker click in Maps

I am trying to get the marker that is clicked but i'm only getting the last marker name
I wish to apply dialog box on the selected marker but it's getting the last marker only
for(int i=0;i<objectResults.length();i++){
JSONObject place=objectResults.getJSONObject(i);
String store_id=place.getString("id");
final String place_name=place.getString("name");
double latitude1, longitude1;
latitude1=place.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
longitude1=place.getJSONObject("geometry").getJSONObject("location").getDouble("lng");
MarkerOptions markerOptions=new MarkerOptions();
LatLng latLng=new LatLng(latitude1,longitude1);
markerOptions.position(latLng);
markerOptions.title(place_name);
mMap.addMarker(markerOptions);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),"YOU CLICKED ON "+place_name,Toast.LENGTH_LONG).show();
return false;
}
);
}
thanks in Advance.
You should get the title of the clicked marker using getTitle() where is storage place_name. like this:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),"YOU CLICKED ON "+marker.getTitle(),Toast.LENGTH_LONG).show();
return false;
}
);
}

How to save a marker onMapClick?

Hi I have this code which adds a marker when I click on the map but if i rerun the app the marker disappears. Is there any way that I can store the marker somehow and then display it ? I have read about shared preferences but I can't provide the code for it. How can I save the onmapclick action in shared preferences and then display it ?Anyone can help me out ?
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
#Override
public void onMapClick(LatLng point) {
gMap.addMarker(new MarkerOptions().position(point));
}
});
Check this link if you still need any help.
Marker m = null;
SharedPreferences prefs = null;//Place it before onCreate you can access its values any where in this class
// onCreate method started
prefs = this.getSharedPreferences("LatLng",MODE_PRIVATE);
//Check whether your preferences contains any values then we get those values
if((prefs.contains("Lat")) && (prefs.contains("Lng"))
{
String lat = prefs.getString("Lat","");
String lng = prefs.getString("Lng","");
LatLng l =new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
gMap.addMarker(new MarkerOptions().position(l));
}
Inside your onMapClick
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
#Override
public void onMapClick(LatLng point) {
marker = gMap.addMarker(new MarkerOptions().position(point));
/* This code will save your location coordinates in SharedPrefrence when you click on the map and later you use it */
prefs.edit().putString("Lat",String.valueOf(point.latitude)).commit();
prefs.edit().putString("Lng",String.valueOf(point.longitude)).commit();
}
});
To remove marker
gMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
//Your marker removed
marker.remove();
return true;
}
});
How to create custom marker with your own image
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lng);
// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_own_image)));
// adding marker
gMap.addMarker(marker);

MarkerOptions tap on info box

I have application with google map v2 containing couple of MarkerOptions inside it. Is there a way to catch tap on info box that is opened after pin is tapped?
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// do something
}
});
Probably duplicate question.
Try out as below:
GoogleMap mMap;
Marker myMarker= mMap.addMarker(new MarkerOptions()
.position(lng)
.title("Head Quarter Office")
.snippet("Delhi")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
mMap.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(arg0.getTitle().equals("Marker")) // if marker source is clicked
Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
return true;
}
});

Categories

Resources