Android 使用谷歌地图v2获取经度和纬度
如何使用Google maps v2获取当前手机位置的经度 我使用此方法缩放我的设备位置:Android 使用谷歌地图v2获取经度和纬度,android,google-maps-android-api-2,Android,Google Maps Android Api 2,如何使用Google maps v2获取当前手机位置的经度 我使用此方法缩放我的设备位置: googleMap.getUiSettings().setMyLocationButtonEnabled(true); 现在我如何得到坐标 我知道获取面积的方法: double left = vr.latLngBounds.southwest.longitude; double top = vr.latLngBounds.northeast.latitude; ...
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
现在我如何得到坐标
我知道获取面积的方法:
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude; ...
使用以下命令:
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;
}
private boolean gps_enabled=false;
专用布尔网络_enabled=false;
私人位置;
私有void getMyCurrentLocation(){
双MyLat=null,MyLong=null;
字符串CityName=“”;
字符串StateName=“”;
字符串CountryName=“”;
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
LocationListener locListener=新建MyLocationListener();
试一试{
gps_已启用=本地管理器
.isprovidenabled(LocationManager.GPS\U提供商);
}捕获(例外情况除外){
}
试一试{
网络\u已启用=本地管理器
.isProviderEnabled(LocationManager.NETWORK_提供商);
}捕获(例外情况除外){
}
//如果未启用任何提供程序,则不要启动侦听器
//如果(!gps_已启用&&!网络_已启用)
//返回false;
如果(gps_已启用){
LocationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,
0,本地侦听器);
}
如果(gps_已启用){
位置=本地管理器
.getLastKnownLocation(LocationManager.GPS\U提供商);
}
if(网络已启用&&location==null){
LocationManager.RequestLocationUpdate(LocationManager.NETWORK\u提供程序,
0,0,1);
}
if(网络已启用&&location==null){
位置=本地管理器
.getLastKnownLocation(LocationManager.网络提供商);
}
如果(位置!=null){
MyLat=location.getLatitude();
MyLong=location.getLongitude();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(新LatLng(
迈拉特(MyLat,MyLong),15);
}否则{
位置loc=getLastKnownLocation(此);
如果(loc!=null){
MyLat=loc.getLatitude();
MyLong=loc.getLongitude();
}
}
locManager.removeUpdates(locListener);//删除定期更新
}
公共类MyLocationListener实现LocationListener{
已更改位置上的公共无效(位置){
如果(位置!=null){
}
}
公共无效onProviderDisabled(字符串提供程序){
}
公共无效onProviderEnabled(字符串提供程序){
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
}
公共静态位置getLastKnownLocation(上下文){
位置=空;
LocationManager LocationManager=(LocationManager)上下文
.getSystemService(“位置”);
List=locationmanager.getAllProviders();
布尔i=假;
迭代器迭代器=list.Iterator();
做{
如果(!iterator.hasNext())
打破
字符串s=(字符串)迭代器.next();
如果(i!=false&&!locationmanager.isProviderEnabled)
继续;
Location location1=locationmanager.getLastKnownLocation;
如果(位置1==null)
继续;
如果(位置!=null){
float f=location.getAccurance();
float f1=location1.getAccurance();
如果(f>=f1){
long l=location1.getTime();
long l1=location.getTime();
如果(l-l1使用:
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;
}
private boolean gps_enabled=false;
专用布尔网络_enabled=false;
私人位置;
私有void getMyCurrentLocation(){
双MyLat=null,MyLong=null;
字符串CityName=“”;
字符串StateName=“”;
字符串CountryName=“”;
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
LocationListener locListener=新建MyLocationListener();
试一试{
gps_已启用=本地管理器
.isprovidenabled(LocationManager.GPS\U提供商);
}捕获(例外情况除外){
}
试一试{
网络\u已启用=本地管理器
.isProviderEnabled(LocationManager.NETWORK_提供商);
}捕获(例外情况除外){
}
//如果未启用任何提供程序,则不要启动侦听器
//如果(!gps_已启用&&!网络_已启用)
//返回false;
如果(gps_已启用){
LocationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,
0,本地侦听器);
}
如果(gps_已启用){
位置=本地管理器
.getLastKnownLocation(LocationManager.GPS\U提供商);
}
if(网络已启用&&location==null){
LocationManager.RequestLocationUpdate(LocationManager.NETWORK\u提供程序,
0,0,1);
}
if(网络已启用&&location==null){
位置=本地管理器
.getLastKnownLocation(LocationManager.网络提供商);
}
如果(位置!=null){
MyLat=location.getLatitude();
MyLong=location.getLongitude();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(新LatLng(
迈拉特(MyLat,MyLong),15);
}否则{
位置loc=getLastKnownLocation(此);
如果(loc!=null){
MyLat=loc.getLatitude();
MyLong=loc.getLongitude();
}
}
locManager.removeUpdates(locListener);//删除定期更新
}
公共类MyLocationListener实现LocationListener{
已更改位置上的公共无效(位置){
如果(位置!=null){
}
}
公共无效onProviderDisabled(字符串提供程序){
}
公共无效onProviderEnabled(字符串提供程序){
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
}
公共静态位置getLastKnownLocation(上下文){
位置=空;
LocationManager LocationManager=(LocationManager)上下文
.getSystemService(“位置”);
列表=低
LatLng latLng = map.getCameraPosition().target;
double lat = latLng.latitude;
double lng = latLng.longitude;