您的位置:首页 > 其它

根据设备定位所得的经纬度,获得具体的地址

2011-09-28 20:33 435 查看

关于android2.2地图定位的问题,程序员取得了设备所在经纬度,得不到具体的地址。这里做一下总结。 转自:http://blog.csdn.net/sun6223508/archive/2011/06/21/6559384.aspx

List<Address> addList = null;
Geocoder ge = new Geocoder(activity);
try {
addList = ge.getFromLocation(经度, 纬度,1);
} catch (IOException e) {
// TODO Auto-generated catch block
if("sdk".equals( Build.PRODUCT )) {
Log.d(TAG, "Geocoder doesn't work under emulation.");
addList= ReverseGeocode.getaddressLocation(location.getLatitude(), location.getLongitude(), 3);
} else{
e.printStackTrace();
}
}


public static List<Address> getaddressLocation(double lat, double lon, int maxResults) {
String url = "http://maps.google.com/maps/geo?q=" + lat + "," + lon + "&output=json&sensor=false";
String response = "";
List<Address> results = new ArrayList<Address>();
HttpClient client = new DefaultHttpClient();

try {
HttpResponse hr = client.execute(new HttpGet(url));
HttpEntity entity = hr.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String buff = null;
while ((buff = br.readLine()) != null)
response += buff;
} catch (IOException e) {
e.printStackTrace();
}
JSONArray responseArray = null;
try {
JSONObject jsonObject = new JSONObject(response);
responseArray = jsonObject.getJSONArray("Placemark");
} catch (JSONException e) {
return results;
}

for(int i = 0; i < responseArray.length() && i < maxResults-1; i++) {
Address addy = new Address(Locale.getDefault());
try {
JSONObject jsl = responseArray.getJSONObject(i);
String addressLine = jsl.getString("address");
if(addressLine.contains(","))
addressLine = addressLine.split(",")[0];
addy.setAddressLine(0, addressLine);
jsl = jsl.getJSONObject("AddressDetails").getJSONObject("Country");
addy.setCountryName(jsl.getString("CountryName"));
addy.setCountryCode(jsl.getString("CountryNameCode"));
jsl = jsl.getJSONObject("AdministrativeArea");
addy.setAdminArea(jsl.getString("AdministrativeAreaName"));
jsl = jsl.getJSONObject("Locality");
addy.setLocality(jsl.getString("LocalityName"));
addy.setThoroughfare(jsl.getJSONObject("Thoroughfare").getString("ThoroughfareName"));
} catch (JSONException e) {
e.printStackTrace();
continue;
}
results.add(addy);
}
return results;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐