我正在将国家/地区名称 字符串格式转换为经度和纬度。
为此我使用 JSON Web服务。
https://maps.googleapis.com/maps/api/geocode/json?address=India&sensor=true
我不知道如何从该JSON获得经度和纬度。
请帮我从上述网络服务中获取经度和纬度。
我曾尝试过以下代码,但我不确定它是否正确:
try
{
Log.v("MAPS","Lang-Lat::1");
String myjsonstring = stringBuffer.toString();
Log.v("My JSON STRING",""+myjsonstring);
Log.v("MAPS","Lang-Lat::2");
JSONArray jsonArray = new JSONArray(myjsonstring);
Log.v("MAPS","Lang-Lat::3");
JSONObject jsonObj = null;
String status = jsonObj.getString("status");
Log.v("MAPS","Lang-Lat::4");
if (status.equals("OK"))
{
Log.v("Log Receive","Json String:::"+myjsonstring);
jsonObj = jsonArray.getJSONObject(0);
JSONObject JSONObject2=jsonObj.getJSONObject("geometry");
JSONObject JSONObject3=JSONObject2.getJSONObject("bounds");
JSONObject JSONObject4=JSONObject3.getJSONObject("northeast");
for(int i=0; i<JSONObject4.length();i++)
{
lang = JSONObject4.getDouble("lng");
lat=JSONObject4.getDouble("lat");
Log.v("Lang:",""+lang);
Log.v("Lat",""+lat);
}
}
}
catch (Exception e)
{
Log.v("Map_View::","Call JSON Exception in Getting Map--->"+e.toString());
e.printStackTrace();
}
任何帮助都会得到赞赏。
先谢谢你。
答案 0 :(得分:1)
public class ParseLatitudeLng {
public 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) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
}
/ * ** * ** * * 获取LAtitude&amp;地址经度 * ** * ** * ** /
public class GetLatLngAsyncTask extends AsyncTask<ArrayList<String>, Void, String>{
JSONObject jObject;
JSONObject places = null;
String lat;
protected void onPreExecute() {
}
@Override
protected String doInBackground(ArrayList<String>... params) {
//ParseLatitudeLng placeJsonParser = new ParseLatitudeLng();
ArrayList<String> inSync=params[0];
double lng1 = 0,lat1 = 0;
try{
lng1=lat1=0;
places = ParseLatitudeLng.getLocationInfo(YOUR_ADDRESS);
lng1 = ((JSONArray)places.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat1 = ((JSONArray)places.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
}catch(Exception e){
Log.d("Exception",e.toString());
}
return lat;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
调用Asynctask获取纬度和放大器经度