Hello everyone I am using custom marker image in google map. My problem is that when i click on that marker or zoom in or zoom out this marker duplicates means, more markers appears on the same location on different points. why this happening? how to avoid it?
Thanks in advance.
Code
#Override
public void onLocationChanged(Location location)
{
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
MarkerOptions markerOptions = new MarkerOptions();
// Setting position for the marker
markerOptions.position(latLng);
// Setting custom icon for the marker
// markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.compass_needle));
// Setting title for the infowindow
// Adding the marker to the map
googleMap.addMarker(markerOptions);
// markerOptions.rotation(260);
}
You are adding markers but you are not clearing previous added loation marker so You have to clear previously added marker when Location is changed again.
Add this line above googleMap.addMarker(markerOptions);
googleMap.clear();
googleMap.addMarker(markerOptions);
I hope it helps!
More correct is to remove a specific marker, and don't clear the whole map.
Because you can have others markers and polylines on map.
In your case you have only one marker showing user location.
You can get it when adding.
private Marker mLocationMarker;
public void onLocationChanged(Location location) {
...
if(mLocationMarker != null) {
mLocationMarker.remove();
}
mLocationMarker = googleMap.addMarker(markerOptions);
}
Related
I was setting mMap.setMyLocationEnabled(true) and I am successfully getting my current location but...
Not able to zoom to that position.
Not able to get lat and lng from that position
From getting latitude and longitude I am using below code but it gives me null. I am calling below method from onMapReady(Google Map) and passing google map object.
private void goToCurrentLocation(GoogleMap map) {
Log.d("Tag", "inside");
Location loc = map.getMyLocation();
if (loc != null) {
LatLng point = new LatLng(loc.getLatitude(), loc.getLongitude());
Log.d("Tag=", "latlng=" + point);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 15));
}
}
Is there any alternative to getting latitude and longitude as getMyLocation deprecated?
use link provided in the answer to get current location and then zoom over that location using below code:
latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
mGoogleMap.animateCamera(cameraUpdate);
http://blog.teamtreehouse.com/beginners-guide-location-android
I have a eight different Longitude and latitude values in that any one value have longitude and latitude and remaining wont have values in that case I have to show a radius for that one value. Can anyone help me to solve this problem.
Thanks
GoogleMap has method addCircle so your just need to call it with appropriate params:
GoogleMap map;
MarkerOptions markerOptions = new MarkerOptions();
LatLng position = new LatLng(50, 50);
markerOptions.position(position);
Marker marker = map.addMarker(markerOptions);
Circle circle = map.addCircle(
new CircleOptions()
.center(position)
.radius(100)
.strokeWidth(0f)
.fillColor(getActivity().getResources().getColor(R.color.someColor))
);
Here I am getting the location, by writing hardcoded latitude and longitude. I used map.setMyLocationEnabled(true);, it gives me the current location in map. But I also want marker to reach upto the my current location.
Can you help to get latitude and longitude of my current position.
private final LatLng LOCATION = new LatLng(22.7515085, 75.8860726);
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(LOCATION).title("It is my current position"));
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION, 6);
map.animateCamera(update);
I am adding markers on my map by fetching user locations stored in the remote server. The locations are displayed but within 3 seconds, the marker disappears. Any solution to this?? Below is my complete code.
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
for(Users u:locList)
{
MarkerOptions markerOptions = new MarkerOptions();
double latitude1 = u.getLatitude();
double longitude1 = u.getLongitude();
LatLng latLng1 = new LatLng(latitude1, longitude1);
// Animating to the touched position
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng1));
if(userMarker!=null) userMarker.remove();
markerOptions = new MarkerOptions().position(new LatLng(latitude1, longitude1)).title(latLng1.toString());
// adding marker
userMarker = mGoogleMap.addMarker(markerOptions);
//userMarker.setVisible(true);
// Placing a marker on the touched position
mGoogleMap.addMarker(markerOptions);
markerOptions.visible(true);
}
}
please check your code do you use something like map.clear()
remove
userMarker.remove();
from your code...
I fixed the same issue by commenting out map.clear. Wow correct my grammar way to go get a life
How can I get the Latitude and Longitude values of a particular location that I have long clicked on the map in Android?
For the long click, I suggest you check out http://www.kind-kristiansen.no/2010/handling-longpresslongclick-in-mapactivity/. This will go into detail on how to listen for long click events within the Maps API since there is little or no built-in functionality that I know of.
As for the lat/lng code, after you get the long click you can translate the pixels to coordinates.
public void recieveLongClick(MotionEvent ev)
{
Projection p = mapView.getProjection();
GeoPoint geoPoint = p.fromPixels((int) ev.getX(), (int) ev.getY());
// You can now pull lat/lng from geoPoint
}
You'll have to manage the LongClick event, and then use the code to find out longitude and latitude with the following code:
GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();
where 'event' is the object of 'MotionEvent'.
Use any other event according to your case.
It gives latitude and longitude on which point of map click
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
//myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
//The code below demonstrate how to convert between LatLng and Location
//Convert LatLng to Location
Location location = new Location("Test");
location.setLatitude(point.latitude);
location.setLongitude(point.longitude);
location.setTime(new Date().getTime()); //Set time as current Date
txtinfo.setText(location.toString());
//Convert Location to LatLng
LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(newLatLng)
.title(newLatLng.toString());
map.addMarker(markerOptions);
}
});