I have a map that will only allow 1 marker at a time. But in order to stop that marker being accidentally replaced with another, it needs to be set so that the marker is cleared before another marker can be added.
I am setting the marker via onMapClick and clearing them via onMapLongClick. As it stands at the moment, the clearing and adding of markers works fine, but I cannot seem to set up the map so that you need to clear the map first.
I have tried the solution from check for existing markers but it hasn't worked.
Here is my code for the setup, which currently works by clearing existing marker and adding another without the need to clear the original marker first:
#Override
public void onMapLongClick(LatLng position) {
mMap.clear();
Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
position = null;
}
#Override
public void onMapClick(LatLng position){
if (position != null){
mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
}
else {
mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
}
}
But I thought it should be something like:
#Override
public void onMapLongClick(LatLng position) {
mMap.clear();
Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
position = null;
}
#Override
public void onMapClick(LatLng position){
if (position == null){
//mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
}
else {
Toast.makeText(this, "Clear first", Toast.LENGTH_SHORT).show();
}
}
But all that does is give me the Toast message and not be able to add a marker at all even on first load of the map.
Any help would be great
Create a reference to the added marker, and remove it before adding it again on a new LatLng position.
private Marker marker;
/**
**
some code here
**
**/
#Override
public void onMapClick(LatLng position) {
if(marker != null)
marker.remove();
marker = mMap.addMarker(new MarkerOptions().position(position).icon(
BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher_new)));
}
I realised I was using code incorrectly as such I have fixed the issue as thus:
public void onMapClick(LatLng position){
if(marker == null) {
marker = mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
} else {
Toast.makeText(this, "Please clear previous position before adding another", Toast.LENGTH_SHORT).show();
}
The app now does as intended. Thank you for your help
Related
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;
}
});
}
I try to make a if statement for a GoogleMap OnInfoWindowClickListener.
But everytime I come to the else.
And hint?
Marker s780 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(52.16033, 7.87964))
.title("780")
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_780)));
Marker s149 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(52.60861, 7.99206))
.title("149")
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_149)));
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker.equals("s780"))
Toast.makeText(getActivity(), "Marker 727", Toast.LENGTH_SHORT).show();
if (marker.equals("s149"))
Toast.makeText(getActivity(), "Marker 725", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(), "Argument:"+marker, Toast.LENGTH_SHORT).show();
In your code: if (marker.equals("s780")), marker is of type Marker, while "s780" is of type String. Of course equals returns false. You should change it to something like:
final Marker s780 = ...
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker == s780) {
...
}
}
}
You should work with the getId() function of the Marker class.
For example inside the :
public void onInfoWindowClick(Marker marker) {
StringBuilder builder = new StringBuilder(marker.getId());
String str = builder.deleteCharAt(0).toString() ;
int id = Integer.parseInt(str) ;
if (id == 0)
Toast.makeText(getActivity(), "Marker 780 was pressed", Toast.LENGTH_LONG).show();
}
Then you can do a simple comparison with ints which is much easier.
The ids of the markers are in order of when they were added to the map, starting by 0. So your marker s780 has the id 0 and the marker s149 has the id 1.
I hope that helped
You just missed an else, so even if the first if (s780) was true, you would make a Toast with
Marker 727
then check again for if (s149), but eventually go inside the else and make a Toast with
Argument:s780
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker.equals("s780"))
Toast.makeText(getActivity(), "Marker 727", Toast.LENGTH_SHORT).show();
else //You missed this else
if (marker.equals("s149"))
Toast.makeText(getActivity(), "Marker 725", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(), "Argument:"+marker, Toast.LENGTH_SHORT).show();
I have found a way for me:
if (marker.getTitle().equals("555 Test"))
{ Toast.makeText(getActivity(), "Marker 5", Toast.LENGTH_SHORT).show();}
Thank you for your hinds.
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();
}
}
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);
I want to add a marker on map with long press.
Toast in onMapClick() was display with normal tap. But long press is not working. Toast in onMapLongClick() is not displayed with long press. Also marker is not displayed on map.
I'm using SupportMapFragment because I want to use my application on Android 2.x devices. I have tested my app on Nexus One which has Android version 2.3.7.
This is my code.
public class MainActivity extends FragmentActivity implements
OnMapClickListener, OnMapLongClickListener {
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager
.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
}
#Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
.show();
} else {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
RQS_GooglePlayServices);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onMapClick(LatLng point) {
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
Toast.makeText(getApplicationContext(), point.toString(),
Toast.LENGTH_LONG).show();
}
#Override
public void onMapLongClick(LatLng point) {
myMap.addMarker(new MarkerOptions().position(point).title(
point.toString()));
Toast.makeText(getApplicationContext(),
"New marker added#" + point.toString(), Toast.LENGTH_LONG)
.show();
}
}
How can I solve this ?
I used this. It worked.
GoogleMap gm;
gm.setOnMapLongClickListener(this);
#Override
public void onMapLongClick(LatLng point) {
gm.addMarker(new MarkerOptions()
.position(point)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
You have no icon setted.
Try:
#Override
public void onMapLongClick(LatLng point) {
myMap.addMarker(new MarkerOptions()
.position(point)
.title(point.toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
Toast.makeText(getApplicationContext(),
"New marker added#" + point.toString(), Toast.LENGTH_LONG)
.show();
}
googleMap = supportMapFragment.getMap();
// Setting a click event handler for the map
googleMap.setOnMapClickListener(new 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);
// Clears the previously touched position
googleMap.clear();
// Animating to the touched position
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
googleMap.addMarker(markerOptions);
}
});
OR
mMap.setOnMapLongClickListener(new
GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick (LatLng latLng){
Geocoder geocoder =
new Geocoder(MapMarkerActivity.this);
List<Address> list;
try {
list = geocoder.getFromLocation(latLng.latitude,
latLng.longitude, 1);
} catch (IOException e) {
return;
}
Address address = list.get(0);
if (marker != null) {
marker.remove();
}
MarkerOptions options = new MarkerOptions()
.title(address.getLocality())
.position(new LatLng(latLng.latitude,
latLng.longitude));
marker = mMap.addMarker(options);
}
});