Android 地理编码&逆地理编码(百度、阿里接口对比)

最近的一个项目中需要将坐标值转换为地理位置显示给用户,即将:

113.952078,22.562627

转换为:深圳市南山区科苑北路


目前网上提供这种功能接口的有:
一、百度“车联网API”
官方地址:http://developer.baidu.com/map/wiki/index.php?title=car/api/anti-geocoding

参考示例:

http://api.map.baidu.com/telematics/v3/reverseGeocoding?location=116.3017193083,40.050743859593&coord_type=gcj02&ak=E4805d16520de693a3fe707cdc962045

需要注册并获取开发者密钥(ak);
[quote]每个Key限制5000次/天。若需升级访问次数,请发送邮件。[/quote]
如果你再使用车联网里面的天气等,每次请求都需要计算次数,这样算下来,如果你的用户一天使用5次,那么只能容纳1000用户,这肯定是不行的。办法就是跟官方合作,发邮件吧!

[img]http://dl2.iteye.com/upload/attachment/0100/3816/a803d9d8-cd77-361e-8117-deded29fc991.png[/img]


二、阿里云
http://gc.ditu.aliyun.com/regeocoding?l=39.938133,116.395739&type=001

这个地址是阿里云地图提供的,无需注册,无访问次数限制,推荐!!!

使用样例:

public static void testUrlRes(String lat, String lng) throws IOException {
// type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
String path = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + ","
+ lng + "&type=100";
// 参数直接加载url后面
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("contentType", "GBK");
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) { // 200表示请求成功
InputStream is = conn.getInputStream(); // 以输入流的形式返回
// 将输入流转换成字符串
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}

if (baos.size() < 1) {
System.out.println("坐标请求异常.");
return;
}
// 得出整个坐标反馈信息
String jsonString = baos.toString();
System.out.println("jsonString:"+jsonString);

baos.close();
is.close();
// 转换成json数据处理
// {"queryLocation":[39.938133,116.395739],"addrList":[{"type":"doorPlate","status":1,"name":"地安门外大街万年胡同1号","admCode":"110102","admName":"北京市,北京市,西城区,","addr":"","nearestPoint":[116.39546,39.93850],"distance":45.804}]}
}

}


三、高德
高德被阿里收购之后已经是一家了,后面肯定会以这个接口为准,上面阿里地图的那个地图后期估计会无法使用;
官方地址:http://lbs.amap.com/api/android-sdk/guide/geocode/
需要注册并获取key,其他没什么集成难度,按照说明文档进行即可;


最后提供一个联网获取json数据的方法:

public static String getJsonContent() {
String urlString = "";
try {
urlString = URL + "&location="+"&ak=" + APP_KEY;
} catch (Exception e1) {
e1.printStackTrace();
}

try {// 获取HttpURLConnection连接对象
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(8000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200) {
return ConvertStream2Json(httpConn.getInputStream());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

private static String ConvertStream2Json(InputStream inputStream) {
String jsonStr = "";
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
try {
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return jsonStr;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值