android获得所有城市名,android – 从城市名称获取纬度和经度

在我目前的Android应用程序中,我想根据输入的城市名称,街道名称或邮政编码获取地理坐标.我怎么能做到这一点?

最好的祝福

解决方法:

通过网络获得地理坐标

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;

}

标签:android,google-maps

来源: https://codeday.me/bug/20190526/1156972.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值