(java功能篇)百度获取地址经纬度

package com.mohe.map;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class LatitudeUtils {

public static final String KEY_1 = "百度Key";

/**
* 返回输入地址的经纬度坐标 key lng(经度),lat(纬度)
*/
public static Map<String, String> getGeocoderLatitude(String address) {
BufferedReader in = null;
try {
// 将地址转换成utf-8的16进制
address = URLEncoder.encode(address, "UTF-8");

// 如果有代理,要设置代理,没代理可注释
// System.setProperty("http.proxyHost","192.168.1.188");
// System.setProperty("http.proxyPort","3128");

URL tirc = new URL("http://api.map.baidu.com/geocoder?address="
+ address + "&output=json&key=" + KEY_1);

in = new BufferedReader(new InputStreamReader(tirc.openStream(),
"UTF-8"));
String res;
StringBuilder sb = new StringBuilder("");
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
String str = sb.toString();
Map<String, String> map = null;
if (!str.isEmpty()) {
int lngStart = str.indexOf("lng\":");
int lngEnd = str.indexOf(",\"lat");
int latEnd = str.indexOf("},\"precise");
if (lngStart > 0 && lngEnd > 0 && latEnd > 0) {
String lng = str.substring(lngStart + 5, lngEnd);
String lat = str.substring(lngEnd + 7, latEnd);
map = new HashMap<String, String>();
map.put("lng", lng);
map.put("lat", lat);
return map;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

public static void main(String args[]) {
Map<String, String> json = LatitudeUtils
.getGeocoderLatitude("查询地址");
System.out.println("lng : " + json.get("lng"));
System.out.println("lat : " + json.get("lat"));
}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值