您的位置:首页 > 其它

通过经纬度获取地理位置信息

2017-11-17 17:06 633 查看
最近做一个车载设备app,设备获取北斗gps数据上传的到后台,app通过后台提供的经纬度反取地理编码位置,支持Google和百度。

获取地理位置url

//Google
public String getGoogleUrl(boolean isCn, String longitude,String latitude) {
if (!isCn) {
return "http://maps.google.com/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false&language="
+ Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
} else {
return "http://ditu.google.cn/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false&language="
+ Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
}
}

//百度,到百度地图获取ak密钥
public String getBaiduUrl(String longitude,String latitude) {
return "http://api.map.baidu.com/geocoder/v2/?" +
"ak=ahH7ICSO020gfifmGVog5OTimwq" +
"&mcode=DC:B1:26:FF:66:49:62:85:98:16:AE:2A:E8:69:A9:EE:AC:14:3B;com.icar.taxi" +
"&output=json" +
"&pois=0&
4000
amp;location="+latitude+","+longitude;
}


获取详细街道地址

/**
* @return 详细街道地址
*/
public String getAddress(String url){
//定义一个HttpClient,用于向指定地址发送请求
HttpClient client = new DefaultHttpClient();

//向指定地址发送Get请求
HttpGet hhtpGet = new HttpGet(url);
StringBuilder sb = new StringBuilder();
try {
//获取服务器响应
HttpResponse response = client.execute(hhtpGet);
HttpEntity entity = response.getEntity();

if(entity !=null){
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"),8192);
String line =null;
while ((line= reader.readLine())!=null){
sb.append(line +"\n");
}
reader.close();
}

//将服务器返回的字符串转换为JSONObject  对象
JSONObject jsonObject = new JSONObject(sb.toString());
//从JSONObject 中取出location 属性
if (AppConfig.mapType()) {
return jsonObject.optJSONObject("result").optString("formatted_address");
} else {
return jsonObject.optJSONArray("results").optJSONObject(0).optString("formatted_address");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: