1、Android MapView的Geocoder类,国内运行报错
Geocoder geocoder=new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses=geocoder.getFromLocation(latitude, longitude, 1);
StringBuilder stringBuilder=new StringBuilder();
if(addresses.size()>0){
Address address=addresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++){
stringBuilder.append(address.getAddressLine(i)).append("\n");
}
stringBuilder.append(address.getLocality()).append("_");
stringBuilder.append(address.getPostalCode()).append("_");
stringBuilder.append(address.getCountryCode()).append("_");
stringBuilder.append(address.getCountryName()).append("_");
System.out.println(stringBuilder.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "报错", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
2、
http://maps.google.com/maps/api/geocode/json?latlng=22.248586104419488,114.17447027648929&language=EN
http://maps.google.cn/maps/api/geocode/json?latlng=22.248586104419488,114.17447027648929&language=EN
经纬度查询的网站
http://www.gpsspg.com/maps.htm
http://www.earthol.com/
public class GetCityTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... urls) {
try {
latitude = "14.550519421017787";
longitude = "121.12469610656741";
String url = "http://maps.google.cn/maps/api/geocode/json?latlng=" + latitude + "," + longitude
+ "&language=zh";
HttpClient dhc = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
dhc.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
dhc.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
HttpResponse httpResponse;
String result = "";
try {
httpResponse = dhc.execute(request);
} catch (ClientProtocolException e) {
return "";
} catch (IOException e) {
// connection is refused
return "";
}
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
}
return result;
} catch (Exception ex) {
return "";
}
}
@Override
protected void onPostExecute(String result) {
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
JSONArray resultArray = jsonObject.getJSONArray("results");
if (resultArray.length() > 0) {
JSONObject obj = resultArray.getJSONObject(0);
String address = obj.getString("formatted_address");
Log.e("address", address);
JSONArray addressArray = obj.getJSONArray("address_components");
for(int i=0;i < addressArray.length();i++){
JSONObject addressObj = addressArray.getJSONObject(i);
JSONArray typeArray = addressObj.getJSONArray("types");
if("locality".equals(typeArray.get(0))){
Log.e("long_name", addressObj.getString("long_name")+"");
}
if("administrative_area_level_1".equals(typeArray.get(0))){
Log.e("long_name", addressObj.getString("long_name")+"");
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}