android获得当前定位的城市名

首先要从安卓获得经纬度:

/** * 获取当前经纬度 */ private void getGPSLocation(){ double latitude = 0.0, longitude = 0.0; LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATIONSERVICE); if(locationManager.isProviderEnabled(LocationManager.GPSPROVIDER)){ Location location = locationManager.getLastKnownLocation(LocationManager.GPSPROVIDER); if(location != null){ latitude = location.getLatitude(); longitude = location.getLongitude(); }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(LocationManager.NETWORKPROVIDER,1000, 0, locationListener); location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(location != null){ latitude = location.getLatitude(); longitude = location.getLongitude(); } } LogUtils.e("Lat:"+latitude+";Lon="+longitude); } }

别忘了加权限

通过百度的接口将经纬度得到城市名

http://api.map.baidu.com/geocoder?location=30.990998,103.645966&output=json&key=28bcdd84fae25699606ffad27f8da77b

如果想要得到xml只要将json转化为xml就好了

{ "status":"OK", "result":{ "location":{ "lng":103.645966, "lat":30.990998 }, "formattedaddress":"四川省成都市都江堰市景中路201号", "business":"幸福", "addressComponent":{ "city":"成都市", "direction":"near", "distance":"2", "district":"都江堰市", "province":"四川省", "street":"景中路", "streetnumber":"201号" }, "cityCode":75 } }