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);
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 made an app utilizing Google Maps. I have added a marker of my current location in it. However, as soon as I open the app, I want it to zoom in to my location instead of showing the whole world at the 95302 23535919711008489 location first. How do I accomplish this?
Use animateCamera() as below
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
Please read the docs
LatLng cameraLatLng = new LatLng(savedLat, savedLng);
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
googleMap.animateCamera(CameraUpdateFactory
.newLatLngZoom(cameraLatLng, 7))
savedLat, savedLng your current location longitude and latitude.You can change zoom number ( 7 ) to your fitting.
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);
}
I am using the google maps in my application, hence I am getting the current location using latitude and longitude, hence I am getting this location where I am going or driving. Hence, I am able to plot the current location on the map, but I don't how to draw the path using this latitude and longitude position. Hence, I am getting the current location every 5min hence I have to draw the path from where the location started to still in the current location.
Please help me. Thanks in advance.
First you have to store all position which you are getting in every 5 minutes then you can use following code to draw line on map:
ArrayList<LatLng> points = new ArrayList<LatLng>();
PolylineOptions lineOptions = null;
lineOptions = new PolylineOptions();
LatLng position1 = new LatLng(lat,long);
points.add(position1);
LatLng position2 = new LatLng(lat,long);
points.add(position2);
lineOptions.addAll(points);
lineOptions.width(5);
gMap.addPolyline(lineOptions); // gMap is object of GoogleMap
I was trying few tricks using google maps api. I am trying to
Get the lat log of my current location and zoom in on the map.
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
Create a radius around my current location on the google map without the 'dot'. Just a circular radius around my location
Then use Marker to manually tap on a location on the map very close to me and get the lat long.
Basically what I am trying is I need to accurately place a marker on a building near me.
I hope I made sense. Any advise will be helpful.
Thanks
Shashi
You can try this
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng myLoc = null;
myLoc = new LatLng(location.getLatitude(),
location.getLongitude());
CameraUpdate center = CameraUpdateFactory.newLatLng(myLoc); //optional
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12); //optional
googleMap.addMarker(new MarkerOptions()
.position(myLoc)
.title("title you want to show")
.snippet("anything you want to show")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));