版权声明:本文为博主原创文章,转载注明出处http://blog.csdn.net/u013142781
百度地图api提供了很多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/。
博主使用过根据地理根据地理位置中文获取经纬度的api,下面提供Java调用百度api接口的代码,详情可参考百度地图api相关说明:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding。更多功能待读者挖掘啦。
public Map<String, BigDecimal> getLatAndLngByAddress(String addr){
String address = "";
String lat = "";
String lng = "";
try {
address = java.net.URLEncoder.encode(addr,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String url = String.format("http://api.map.baidu.com/geocoder/v2/?"
+"ak=4rcKAZKG9OIl0wDkICSLx8BA&output=json&address=%s",address);
URL myURL = null;
URLConnection httpsConn = null;
//进行转码
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
if ((data = br.readLine()) != null) {
lat = data.substring(data.indexOf("\"lat\":")
+ ("\"lat\":").length(), data.indexOf("},\"precise\""));
lng = data.substring(data.indexOf("\"lng\":")
+ ("\"lng\":").length(), data.indexOf(",\"lat\""));
}
insr.close();
}
} catch (IOException e) {
}
Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
map.put("lat", new BigDecimal(lat));
map.put("lng", new BigDecimal(lng));
return map;
}
时间: 2024-11-03 09:06:03