通过locationManager获取所在城市经纬度
/**
* 获取经纬度
*/
public static Location getLocation(Context context) {
String locationProvider;
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> providers = locationManager.getProviders(true);
if (providers == null) return null;
if (providers.contains(LocationManager.GPS_PROVIDER)) {
locationProvider = LocationManager.GPS_PROVIDER;
} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
locationProvider = LocationManager.NETWORK_PROVIDER;
} else {
Log.i("wxbnb", "getLocation: 没有可用的位置提供器");
return null;
}
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
Log.i("wxbnb", "1纬度:" + location.getLatitude() + "经度:" + location.getLongitude());
return location;
} else {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(locationProvider, 1000, 0, locationListener);
location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
Log.i("wxbnb", "2纬度:" + location.getLatitude() + "经度:" + location.getLongitude());
}
return location;
}
}
通过高德webapi的逆地理位置编码获取当前经纬度的城市信息 。
/**
* 获取经纬度所在城市名字
*
* @param location
*/
public void getCityName(Location location) {
String path = "http://restapi.amap.com/v3/geocode/regeo?output=json&location=" + location.getLongitude() + "," + location.getLatitude() + "&key=xxxxxxx";
todo:网络请求该url即可
}
PS:得通过get方式获取,记得先去高德地图官网的webapi服务注册。