获得经纬度从城市名称 [英] get latitude and longitude from city name
本文介绍了获得经纬度从城市名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我目前的Android应用程序,我想获得的基础上所输入城市名称,街道名称或邮政编码code中的geocordinates。我怎样才能做到这一点?
In my current android application, I would like to get the geocordinates based on an entered city name, street name or zip code. How can I accomplish this?
最好的问候
推荐答案
通过网络获取地理坐标
private static JSONObject getLocationInfo(String address)
{
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
Log.i("getLocationInfo ClientProtocolException", e.toString());
} catch (IOException e) {
Log.i("getLocationInfo IOException", e.toString());
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.i("getLocationInfo JSONException", e.toString());
}
return jsonObject;
}
private static boolean getLatLong(JSONObject jsonObject)
{
try {
longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
Log.i("Log1", longitute1+"");
latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Log.i("lat1", latitude1+"");
} catch (JSONException e) {
longitute=0;
latitude = 0;
Log.i("getLatLong", e.toString());
return false;
}
return true;
}
这篇关于获得经纬度从城市名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文