Reputation: 5850
How do i get longitude longitude of my current phone location using Google maps v2?
I use this method to zoom on my device place:
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
Now how do i get the coordinates?
I know this method to get an area:
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude; ...
Upvotes: 0
Views: 1890
Reputation: 8856
You can get the center Lat and Lng values from google maps via this code
LatLng latLng = map.getCameraPosition().target;
double lat = latLng.latitude;
double lng = latLng.longitude;
Upvotes: 4
Reputation: 2877
The Google Maps API location has listeners, for example:
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new
GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = gMap.addMarker(new MarkerOptions().position(loc));
if(gMap != null){
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
}
};
and then set the listener for the map:
gMap.setOnMyLocationChangeListener(myLocationChangeListener);
This will get called when the map first finds the location. try this code.
Upvotes: 1
Reputation: 3784
Use This:
private boolean gps_enabled = false;
private boolean network_enabled = false;
private Location location;
private void getMyCurrentLocation() {
Double MyLat = null, MyLong = null;
String CityName = "";
String StateName = "";
String CountryName = "";
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new MyLocationListener();
try {
gps_enabled = locManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
// if(!gps_enabled && !network_enabled)
// return false;
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locListener);
}
if (gps_enabled) {
location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (network_enabled && location == null) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, locListener);
}
if (network_enabled && location == null) {
location = locManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location != null) {
MyLat = location.getLatitude();
MyLong = location.getLongitude();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
MyLat, MyLong), 15));
} else {
Location loc = getLastKnownLocation(this);
if (loc != null) {
MyLat = loc.getLatitude();
MyLong = loc.getLongitude();
}
}
locManager.removeUpdates(locListener); // removes the periodic updates
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
public static Location getLastKnownLocation(Context context) {
Location location = null;
LocationManager locationmanager = (LocationManager) context
.getSystemService("location");
List list = locationmanager.getAllProviders();
boolean i = false;
Iterator iterator = list.iterator();
do {
if (!iterator.hasNext())
break;
String s = (String) iterator.next();
if (i != false && !locationmanager.isProviderEnabled(s))
continue;
Location location1 = locationmanager.getLastKnownLocation(s);
if (location1 == null)
continue;
if (location != null) {
float f = location.getAccuracy();
float f1 = location1.getAccuracy();
if (f >= f1) {
long l = location1.getTime();
long l1 = location.getTime();
if (l - l1 <= 600000L)
continue;
}
}
location = location1;
i = locationmanager.isProviderEnabled(s);
} while (true);
return location;
}
Upvotes: 0