I am using Google Maps Extension Library. I have this:
int nsize = visibleMarkers.size();
for (int i = 0; i < nsize; i++) {
String title = visibleMarkers.valueAt(i).getTitle();
String desc = visibleMarkers.valueAt(i).getDesc();
Float latitude = visibleMarkers.valueAt(i).getLat();
Float longitude = visibleMarkers.valueAt(i).getLon();
m = map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(title)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.snotel_marker)));
}
and the map gets populated fine with all the markers.
I am trying to add data to a toast to see the description and title from the marker window on click:
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(MainActivity.this,
"Title: " + visibleMarkers.valueAt(i).getTitle(),
Toast.LENGTH_SHORT).show();
}
});
When i add this setOnInfoWindow Listener, i variable needs to be final. I want to get the title of the marker from my visibleMarkers SparseArray, but I just cannot figure out how to get the data from the marker I cam clicking on. I know the desc has info in it, since using a .snippet(desc) shows the info on marker click.
What am I missing here?
EDIT:::
I changed my onPostExecute adding Marker m and my data to another array:
int nsize = visibleMarkers.size();
for (int i = 0; i < nsize; i++) {
MapMarkers marks = new MapMarkers();
String title = visibleMarkers.valueAt(i).getTitle();
String desc = visibleMarkers.valueAt(i).getDesc();
Float latitude = visibleMarkers.valueAt(i).getLat();
Float longitude = visibleMarkers.valueAt(i).getLon();
m = map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(title)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.snotel_marker)));
marks.setTitle(title);
marks.setDesc(desc);
markerInfo.put(m, marks);
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
MapMarkers markInfo = markerInfo.get(marker);
Intent i = new Intent(MainActivity.this,
MarkerInformation.class);
i.putExtra("name", markInfo.getTitle()).putExtra(
"description", markInfo.getDesc());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
}
Does that seem correct?
First of all in your setOnInfoWindowClickListener the "i" has no relation with the marker you have pressed, the only connection with it is the marker object that is passed in onInfoWindowClick method.
you can get the data directly from this marker object which has all the data you need, if you don't want this please explain more your problem
try this
Toast.makeText(MainActivity.this, "Title: " + m.getTitle(), Toast.LENGTH_SHORT).show();
Note that m must be declare outside.
Related
I have a map where i display some markers that are stored in a db (MySQL), for each marker there are some other fields that goes with it (for example name,adress, category, etc.)what i want to do is compare if the field "category" is equals "category A" change the icon of the marker, how can i make this possible? Any idea is appreciated!
I was trying something like this, but it didn't work out:
if(location.get(i).get("campo_categoria").toString()=="Obras publicas")
//if (name=="Obras publicas")
{
new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op));
Main:
ArrayList<HashMap<String, String>> location = null;
String url = "http://appserver.puertovallarta.gob.mx/movil/getLanLong2.php";
try {
JSONArray data = new JSONArray(getHttpGet(url));
location = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("id", c.getString("id"));
map.put("campo_latitud", c.getString("campo_latitud"));
map.put("campo_longitud", c.getString("campo_longitud"));
map.put("campo_categoria", c.getString("campo_categoria"));
location.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String campouno = "";
if(!TextUtils.isEmpty(campouno)){
double campo_latitud = Double.parseDouble(campouno);
}
//campo_latitud = Double.parseDouble(location.get(0).get("Latitude").toString());
String campodos = "";
if(!TextUtils.isEmpty(campodos)){
double campo_longitud = Double.parseDouble(campodos);
}
for (int i = 0; i < location.size(); i++) {
if(!TextUtils.isEmpty(location.get(i).get("campo_latitud").toString())&&!TextUtils.isEmpty(location.get(i).get("campo_longitud").toString())) {
campo_latitud = Double.parseDouble(location.get(i).get("campo_latitud").toString());
campo_longitud = Double.parseDouble(location.get(i).get("campo_longitud").toString());
}
String name = location.get(i).get("campo_categoria").toString();
LatLng downtown = new LatLng(20.663203, -105.228053);
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(campo_latitud, campo_longitud))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.title(name));
if(location.get(i).get("campo_categoria").toString()=="Obras publicas")
//if (name=="Obras publicas")
{
new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op));
}
googleMap.moveCamera(CameraUpdateFactory.newLatLng(downtown));
googleMap.setLatLngBoundsForCameraTarget(ADELAIDE);
}}
PHP file:
<?php
require_once 'dbDetails.php';
$sql = "SELECT * FROM `reportes2` ORDER BY id ASC";
$objQuery = mysqli_query($con,$sql);
$arrRows = array();
$arryItem = array();
while($arr = mysqli_fetch_array($objQuery)) {
$arryItem["id"] = $arr["id"];
$arryItem["campo_latitud"] = $arr["campo_latitud"];
$arryItem["campo_longitud"] = $arr["campo_longitud"];
$arryItem["campo_categoria"] = $arr["campo_categoria"];
$arryItem["campo_descripcion"] = $arr["campo_descripcion"];
$arrRows[] = $arryItem;
}
try this Approach.!
String BLUE_COLOR="blue";
String RED_COLOR="red";
String campoCategoria= location.get(i).get("campo_categoria").toString();
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(campo_latitud, campo_longitud))
.icon(BitmapDescriptorFactory.fromResource(getIconUsingSwitch(campoCategoria)))
.title(name));
private int void getIconUsingSwitch(String campoCategoria) {
switch (campoCategoria) {
case "blue":
return R.drawable.ic_blue;
break;
case "red":
return R.drawable.ic_red;
break;
default:
return R.drawable.ic_normal;
}
}
for String use equalsIgnoreCase and for Object use equals
this will ignore Upper case and lower case BLUE or blue it will run for both
void getIconUsingIf(String campoCategoria) {
if (campoCategoria.equalsIgnoreCase(BLUE_COLOR)) {
return R.drawable.ic_blue;
} else if (campoCategoria.equalsIgnoreCase(RED_COLOR)) {
return R.drawable.ic_red;
} else {
return R.drawable.ic_normal;
}
}
Try this:
LatLng downtown = new LatLng(20.663203, -105.228053);
for (int i = 0; i < location.size(); i++) {
if(!TextUtils.isEmpty(location.get(i).get("campo_latitud").toString())&&!TextUtils.isEmpty(location.get(i).get("campo_longitud").toString())) {
campo_latitud = Double.parseDouble(location.get(i).get("campo_latitud").toString());
campo_longitud = Double.parseDouble(location.get(i).get("campo_longitud").toString());
}
String name = location.get(i).get("campo_categoria").toString();
if (Objects.equals(location.get(i).get("campo_categoria").toString(),"Obras publicas")) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(campo_latitud, campo_longitud))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op1))
.title(name));
} else if (Objects.equals(location.get(i).get("campo_categoria").toString(),"Any other Choice")) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(campo_latitud, campo_longitud))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op2))
.title(name));
}
else {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(campo_latitud, campo_longitud))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.title(name));
}
}
googleMap.moveCamera(CameraUpdateFactory.newLatLng(downtown));
googleMap.setLatLngBoundsForCameraTarget(ADELAIDE);
This way you'll have different markers for different categories and you can use many else-if without any problem. I've 17 in mine and works great.
ALso, to manage all these markers you can create a List as List<Marker> list = new ArrayList<>(); and then can add markers in it as
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(campo_latitud, campo_longitud))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_op1))
.title(name));
list.add(marker);
You can then access any marker from it as:
Marker m = list.get(5); //position of needed marker
m.getTitle or m.getPosition or m.getAlpha //all these will then work
You can also use for loops as well:
for(Marker m:list){
if (m.getTitle().equals("Your Title")) {
m.showInfoWindow();
CameraPosition cameraPosition = new CameraPosition.Builder().target(m.getPosition()).zoom(14).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
break;
}
The above for loop code is directly copied from my project which actually highlights the selected marker from a spinner in my map activity between 150+ markers.
Something I've missed, tell me I can help.
I am adding multiple markers on map. I am trying to show all markers info pannel when markers added on map, but able to show only last markers info. here is my code :
/*Place all stores on map*/
private void placeMarkersOnMap() {
for (int i = 0; i < store_list.size(); i++) {
double latitude = Double.parseDouble(store_list.get(i).latitude.toString());
double longitude = Double.parseDouble(store_list.get(i).longitude.toString());
String title = store_list.get(i).id + " " + store_list.get(i).store_name.toString();
String snipet = store_list.get(i).address + " ," + store_list.get(i).working_hours;
Marker marker = map.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude))
.title(title)
.snippet(snipet).icon(BitmapDescriptorFactory.fromResource(map_marker)));
marker.showInfoWindow();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 14));
}
}
I was trying to get some place lists from the google places and show it in the googlemap api v2. Also i want to show infowindow, But here i can only acess the place reference through the info window argument. And the problem is the infowindow will show the reference too. It is a nasty thing. I don't want to show it inside the infowindow, but i want the reference inside this infowindow clicklistener for passing to the method how do i do that? Any help appreciated...
Code
for(Place place : nearPlaces.results){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting latitude of the place
double latitude = place.geometry.location.lat;
double longitude = place.geometry.location.lng;
// Getting name
String NAME = place.name;
// Getting vicinity
String VICINITY = place.vicinity;
//Reference of a place
String REFERENCE = place.reference;
LatLng latLng = new LatLng(latitude, longitude);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
markerOptions.title(NAME + " : " + VICINITY);
markerOptions.snippet(REFERENCE);
markerOptions.icon(bitmapDescriptor);
// Placing a marker on the touched position
final Marker marker = mGoogleMap.addMarker(markerOptions);
mGoogleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
#Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
arg0.hideInfoWindow();
double dlat=arg0.getPosition().latitude;
double dlon=arg0.getPosition().longitude;
alert.showpickAlertDialog2(PlacesMapActivity.this,dlat , dlon, arg0.getSnippet());
}
}
);
Keep a Map<Marker, Place> & don't put REFERENCE into the snippet.
When the info window is clicked, look up the marker in the map and get the corresponding Place
HashMap<Marker, Place> markerPlaces = new HashMap<Marker, Place>();
for(Place place : nearPlaces.results){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting latitude of the place
double latitude = place.geometry.location.lat;
double longitude = place.geometry.location.lng;
// Getting name
String NAME = place.name;
// Getting vicinity
String VICINITY = place.vicinity;
//Reference of a place
String REFERENCE = place.reference;
LatLng latLng = new LatLng(latitude, longitude);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
markerOptions.title(NAME + " : " + VICINITY);
markerOptions.icon(bitmapDescriptor);
// Placing a marker on the touched position
final Marker marker = mGoogleMap.addMarker(markerOptions);
markerPlaces.put(marker, place);
mGoogleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
#Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
arg0.hideInfoWindow();
double dlat=arg0.getPosition().latitude;
double dlon=arg0.getPosition().longitude;
Place p = markerPlaces.get(marker);
alert.showpickAlertDialog2(PlacesMapActivity.this,dlat , dlon, p.reference);
}
}
);
Or, in this instance, you could make a final reference to place and use that in the onInfoWindowClick:
for(Place place : nearPlaces.results){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting latitude of the place
double latitude = place.geometry.location.lat;
double longitude = place.geometry.location.lng;
// Getting name
String NAME = place.name;
// Getting vicinity
String VICINITY = place.vicinity;
//Reference of a place
String REFERENCE = place.reference;
LatLng latLng = new LatLng(latitude, longitude);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
markerOptions.title(NAME + " : " + VICINITY);
markerOptions.icon(bitmapDescriptor);
// Placing a marker on the touched position
final Marker marker = mGoogleMap.addMarker(markerOptions);
final Place p = place;
markerPlaces.put(marker, place);
mGoogleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
#Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
arg0.hideInfoWindow();
double dlat=arg0.getPosition().latitude;
double dlon=arg0.getPosition().longitude;
alert.showpickAlertDialog2(PlacesMapActivity.this,dlat , dlon, p.reference);
}
}
);
I have map view in my fragment. I need to refresh map and add different markers based on condition. So, I should remove last markers from map before add new markers.
Actually, some weeks ago app was working fine and suddenly it happened. My code is like this:
private void displayData(final List<Venue> venueList) {
// Removes all markers, overlays, and polylines from the map.
googleMap.clear();
.
.
.
}
Last time it was working fine (before new Google Map API announce by Android team in I/O 2013). However, after that I adapted my code to use this new API. Now, I don't know why this method googleMap.clear(); doesn't work!
Any suggestion would be appreciated. Thanks
=======
Update
=======
Complete code:
private void displayData(final List<Venue> venueList) {
// Removes all markers, overlays, and polylines from the map.
googleMap.clear();
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_LEVEL), 2000, null);
// Add marker of user's position
MarkerOptions userIndicator = new MarkerOptions()
.position(new LatLng(lat, lng))
.title("You are here")
.snippet("lat:" + lat + ", lng:" + lng);
googleMap.addMarker(userIndicator);
// Add marker of venue if there is any
if(venueList != null) {
for(int i=0; i < venueList.size(); i++) {
Venue venue = venueList.get(i);
String guys = venue.getMaleCount();
String girls= venue.getFemaleCount();
String checkinStatus = venue.getCan_checkin();
if(checkinStatus.equalsIgnoreCase("true"))
checkinStatus = "Checked In - ";
else
checkinStatus = "";
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude())))
.title(venue.getName())
.snippet(checkinStatus + "Guys:" + guys + " and Girls:" + girls)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_orange_pin));
googleMap.addMarker(markerOptions);
}
}
// Move the camera instantly to where lat and lng shows.
if(lat != 0 && lng != 0)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), ZOOM_LEVEL));
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
});
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
String str = marker.getId();
Log.i(TAG, "Marker id: " + str);
str = str.substring(1);
int markerId = Integer.parseInt(str);
markerId -= 1; // Because first item id of marker is 1 while list starts at 0
Log.i(TAG, "Marker id " + markerId + " clicked.");
// Ignore if User's marker clicked
if(markerId < 0)
return;
try {
Venue venue = venueList.get(markerId);
if(venue.getCan_checkin().equalsIgnoreCase("true")) {
Fragment fragment = VenueFragment.newInstance(venue);
if(fragment != null)
changeFragmentLister.OnReplaceFragment(fragment);
else
Log.e(TAG, "Error! venue shouldn't be null");
}
} catch(NumberFormatException e) {
e.printStackTrace();
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
});
Okay finally I found a replacement way to solve my problem. The interesting problem is when you assign a marker to map, it's id is 'm0'. When you remove it from map and assign new marker you expect the id should be 'm0' but it's 'm1'. Therefore, it showed me the id is not trustable. So I defined List<Marker> markerList = new ArrayList<Marker>(); somewhere in onActivityCreated() of my fragment.
Then changed above code with following one. hope it helps others if they have similar issue with markers.
private void displayData(final List<Venue> venueList) {
Marker marker;
// Removes all markers, overlays, and polylines from the map.
googleMap.clear();
markerList.clear();
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_LEVEL), 2000, null);
// Add marker of user's position
MarkerOptions userIndicator = new MarkerOptions()
.position(new LatLng(lat, lng))
.title("You are here")
.snippet("lat:" + lat + ", lng:" + lng);
marker = googleMap.addMarker(userIndicator);
// Log.e(TAG, "Marker id '" + marker.getId() + "' added to list.");
markerList.add(marker);
// Add marker of venue if there is any
if(venueList != null) {
for (Venue venue : venueList) {
String guys = venue.getMaleCount();
String girls = venue.getFemaleCount();
String checkinStatus = venue.getCan_checkin();
if (checkinStatus.equalsIgnoreCase("true"))
checkinStatus = "Checked In - ";
else
checkinStatus = "";
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude())))
.title(venue.getName())
.snippet(checkinStatus + "Guys:" + guys + " and Girls:" + girls)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_orange_pin));
marker = googleMap.addMarker(markerOptions);
// Log.e(TAG, "Marker id '" + marker.getId() + "' added to list.");
markerList.add(marker);
}
}
// Move the camera instantly to where lat and lng shows.
if(lat != 0 && lng != 0)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), ZOOM_LEVEL));
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
});
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
int markerId = -1;
String str = marker.getId();
Log.i(TAG, "Marker id: " + str);
for(int i=0; i<markerList.size(); i++) {
markerId = i;
Marker m = markerList.get(i);
if(m.getId().equals(marker.getId()))
break;
}
markerId -= 1; // Because first item of markerList is user's marker
Log.i(TAG, "Marker id " + markerId + " clicked.");
// Ignore if User's marker clicked
if(markerId < 0)
return;
try {
Venue venue = venueList.get(markerId);
if(venue.getCan_checkin().equalsIgnoreCase("true")) {
Fragment fragment = VenueFragment.newInstance(venue);
if(fragment != null)
changeFragmentLister.OnReplaceFragment(fragment);
else
Log.e(TAG, "Error! venue shouldn't be null");
}
} catch(NumberFormatException e) {
e.printStackTrace();
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
}
If you want to clear "all markers, overlays, and polylines from the map", use clear() on your GoogleMap.
Use map.clear() to remove all markers from Google map
Suppose there is an ArrayList of 2 locations. Now, you display markers on the map based on that array. There will be two markers. When you click on the first marker it gives you a marker index m0 and the second is m1.
Say that you refresh location array and now you got an array with 3 locations. You got 3 markers. But when you click on the first one, it gives you marker index m2 (as if it continues counting from the first location arraw) the second is m3 and the third is m4. What you actually want is to make it as m0, m1, m2.
Now, when you build you location array you probably call location.add("you location")... and when you rebuild it (refresh it) you call location.clear() first and then build it again.
SOLUTION:
First, make another dummy array similar to location array and build it in for loop together with a real location array: locaionDummy.add(i) but don't you EVER refresh it - that way it keeps building and you will know how many locations you've ever had from the very beginning.
Second, do something like this (example of setting image) with mIndex as int variable:
void locatePins() {
mIndex = locationDummy.size()-location.size();
for (int i = 0; i < userID.size(); i++) {
LatLng pgLocation = new LatLng(Double.parseDouble(latArr.get(i)), Double.parseDouble(lngArr.get(i)));
myMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
View view = getLayoutInflater().inflate(R.layout.map_marker_info, null);
RelativeLayout markerInfo= view.findViewById(R.id.markerInfo);
TextView name = view.findViewById(R.id.userName);
TextView details = view.findViewById(R.id.userInfo);
ImageView img = view.findViewById(R.id.userImg);
name.setText(marker.getTitle());
details.setText(marker.getSnippet());
img.setImageBitmap (bmImg.get(Integer.parseInt(marker.getId().replaceAll("[^\\d.]", ""))-mIndex));
return view;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
// ... the rest of the code
}
}
The key is to subtract the real location.size() from a locationDummy.size() to get a number int mIndex that you will subtract later on from marker.getId()
If you need to remove only the markers, and leave other things such as ground overlay,etc there, use:
marker.remove();
or if you have many:
if(markers!=null&&mMap!=null){
for(int i=0;i<markers.size();i++){
markers.get(i).remove();
}
}
where
List<Marker> markers = new ArrayList<>();
I am adding a maker on touch of map and want to remove that marker on click of some button but that marker is not removing from map .Here is my Code
// Marker of end Point
Marker endPointMarker;
onclick of map
#Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
double lat = point.latitude;
double lng = point.longitude;
// Add marker of destination point
try {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(BookCabScreen.this);
if (lat != 0 || lng != 0) {
addresses = geocoder.getFromLocation(lat, lng, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
Log.d("TAG", "address = " + address + ", city =" + city
+ ", country = " + country);
endPointMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address));
markers.add(mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address)));
btnStartUp.setEnabled(true);
btnStopPoint.setEnabled(true);
mJbBookCab.setEndPointLat(lat);
mJbBookCab.setEndPointLng(lng);
} else {
Toast.makeText(BookCabScreen.this,
"latitude and longitude are null",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
on click of button
if (endPointMarker != null) {
endPointMarker.remove();
endPointMarker = null;
}
But it is not removing from map ?Please help
You are adding same marker twice:
endPointMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address));
markers.add(mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address)));
Just remove one call to GoogleMap.addMarker.
what you are doing is correct but if this is not working then You can use mMap.clear() inside your onclick method this will remove all the markers or if you want only a specific marker not to be shown then you can use endPointMarker.setVisible(false)