How to put marker on android map location? - android

After I find this Location mCurrentLocation = mLocationClient.getLastLocation(); how can I add a marker to that that location?
Making a marker like
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude))
requires a position object, is there a way that I can retrieve a position from the Location object? or is this done in some other way?

You can use the getLatitude() and getLongitude() of the Location class which will return double which by then you can pass it in your new LatLong constructor.
sample:
MarkerOptions marker = new MarkerOptions().position(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))

Related

Update Marker position with Animation in Android Studio

I have successfully fetched the Latitude and Longitude from some data stored in the Database. But I am unable to update the marker location on the map. Can please someone help me with the code or as to how to do that.
Try this using CameraUpdate ,
LatLng coordinate = new LatLng(lat, lng); //Store these lat lng values somewhere. These should be constant.
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(coordinate, 15);
mMap.animateCamera(location);
Or can also use this,
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(totalAddress); //Here Total Address is address which you want to show on marker
mMap.clear();
markerOptions.icon(
BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions.getPosition();
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

Android Google Map showing multiple markers on current location

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);
}

Look if current location is near marker

I have a Google Maps API Activity and want the markes only to be clickable if the user is in a certain radius to them.
How is the best way to do this?
This is how I set my markers:
Marker marker1 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793012, 9.926201))
.title(getString(R.string.Title1))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker2 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.792742, 9.939118))
.title(getString(R.string.Title2))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker3 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793349, 9.932558))
.title(getString(R.string.Title3))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Solution code:
LatLng markerPosition = marker.getPosition();
myLocation = locationManager.getLastKnownLocation(provider);
Location markerLoc = new Location("marker");
markerLoc.setLatitude(markerPosition.latitude);
markerLoc.setLongitude(markerPosition.longitude);
float meters = myLocation.distanceTo(markerLoc);
You can get the LatLng of the markers by marker.getPosition(); then you can compare it with the CurrentLocation. To do this, you can check this post, and next you can make clickable or notclickable a marker as follows:
getMap().setOnMarkerClickListener(new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
});
If you return true for the function, it means that you've accepted the occurred click event on your marker; otherwise, you've not accepted it.
Moreover, you can do it with:
MarkerOptions.clickable and Marker.setClickable.
This is just a guide who how I would do what you want. hope it helps. :)

Showing multiple markers in google map v2

i have a set of lat long coordinates. i want to display all of those markers at a time on google map. I know how to display a single marker. But don't know how to display multiples at once. Anybody please help me.
Thank you.
i have lat long points.
like
id = 123 lat = 12.00 lon = 77.00
id = 124 lat = 12.01 lon = 77.01
etc.
May This Help You
for(int pin=0; pin<pins.size(); pin++)
{
LatLng pinLocation = new LatLng(Float.parseFloat(pins.get(pin).latitude), Float.parseFloat(pins.get(pin).longitude));
Marker storeMarker = map.addMarker(new MarkerOptions()
.position(pinLocation)
.title(pins.get(pin).pinname)
.snippet(pins.get(pin).address)
);
}
Try this:
LatLng one = new LatLng(2.40744, 77.014702);//Latitude and long points
LatLng two = new LatLng(2.407440, 77.014702);
.......
Similarly u can use more lat and long
myMarkerOne = gm.addMarker(new MarkerOptions()
.position(one)//use LatLng obj
.title("C")
.snippet("dsfd")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
myMarkerTwo = gm.addMarker(new MarkerOptions()
.position(two)
.title("C")
.snippet("dsfds")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
refer this: How to add multiple marker and overlay in v2 google map?
Also check this link:
Create a LatLng object by using following format:
LatLng location = new LatLng(latitude, longitude);
Then call the method with your LatLng object along with your GoogleMap object:
private Marker setCurrentLocation(LatLng location, GoogleMap map) {
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.drawable.user_location_pin);
Marker marker = map.addMarker(new MarkerOptions().position(location)
.icon(bitmapDescriptor).title(location.toString()));
return marker;
}

Android Update Current Location

So here is my method for getting the user saved location and then move camera to that location:
private void updatePlaces(){
locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double lat = lastLoc.getLatitude();
double lng = lastLoc.getLongitude();
LatLng lastLatLng = new LatLng(lat, lng);
if(userMarker!=null) userMarker.remove();
userMarker = theMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title("You are here")
.icon(BitmapDescriptorFactory.fromResource(userIcon))
.snippet("Your last recorded location"));
theMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);
}
How can i modify this code in order to get the new position only once, compare and then get camera to it?
Thank you in advance.
I don't understand you question, if you run the method updatePlaces() only ones it will getLastKnownLocation only once and updates the user marker only once. could you please be more explanatory on what you are trying to achieve?

Categories

Resources