Android 根据名字获取经纬度,Android 根据城市名称获取经纬度

/**

* 根据城市名称获取经纬度

*

* @param city

城市中文名称、拼音、英文

* @return location

经纬度数组,0为经度,1为纬度

*/

private String[] getLocationByCityName(String city) {

String[] location = new String[2];

try {

HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(

"http://maps.google.com/maps/geo?q=" + city);

int res = 0;

res = httpClient.execute(httpPost).getStatusLine()

.getStatusCode();

if (res == 200) {

/*

* 当返回码为200时,做处理 得到服务器端返回json数据,并做处理

*/

HttpResponse httpResponse = httpClient

.execute(httpPost);

StringBuilder builder = new StringBuilder();

BufferedReader bufferedReader2 = new BufferedReader(

new InputStreamReader(httpResponse.getEntity()

.getContent()));

for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2

.readLine()) {

builder.append(s);

}

/**

* 这里需要分析服务器回传的json格式数据,

*/

JSONObject jsonObject = new JSONObject(builder

.toString());

JSONArray jsonArray = jsonObject

.getJSONArray("Placemark");

for (int i = 0; i < jsonArray.length(); i++) {

JSONObject jsonObject2 = (JSONObject) jsonArray

.opt(i);

JSONObject jsonObject3 = new JSONObject(jsonObject2

.getString("Point"));

JSONArray jsonArray1 = jsonObject3

.getJSONArray("coordinates");

location[0] = (String) jsonArray1.get(0);

location[1] = (String) jsonArray1.get(1);

}

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (JSONException e) {

e.printStackTrace();

}

return location;

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值