百度地图地址经纬度互转,java代码(v2版本)
2017-03-07 00:00
531 查看
摘要: 百度地图web最新版
2个星期前用百度地图做了地址转换后。本来准备写个工具类添加到项目中突然发现不能用了,于是上百度地图api官网,发现接口已经更改了升级为v2,修改后可以使用,于是把工具类进行分享。
工具类:BaiDuMapUnit
测试:
2个星期前用百度地图做了地址转换后。本来准备写个工具类添加到项目中突然发现不能用了,于是上百度地图api官网,发现接口已经更改了升级为v2,修改后可以使用,于是把工具类进行分享。
工具类:BaiDuMapUnit
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import org.springframework.util.StringUtils; public class BaiDuMapUnit { /** * 输入地址返回经纬度坐标 key lng(经度),lat(纬度) */ public static void getGeocoderLatitude(String address) { BufferedReader in = null; try { address = URLEncoder.encode(address, "UTF-8"); URL tirc = new URL("http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + "wws9Qu73jw4QkOL6osEyIsA9Yob2yYgR"+"&callback=showLocation"); 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(); if (!StringUtils.isEmpty(str)) { 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); System.out.println("lng:" + lng + " lat:" + lat); } } } catch (Exception e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 输入经纬度返回地址 key lng(经度),lat(纬度) */ public static void getPosition(String latitude, String longitude) throws MalformedURLException { BufferedReader in = null; URL tirc = new URL("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + latitude + "," + longitude + "&output=json&pois=1&ak=" + "wws9Qu73jw4QkOL6osEyIsA9Yob2yYgR"); try { 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(); System.out.println(str); if (!StringUtils.isEmpty(str)) { int lngStart = str.indexOf("formatted_address\":\""); int lngEnd = str.indexOf("\",\"business"); if (lngStart > 0 && lngEnd > 0 ) { String ads = str.substring(lngStart + 20, lngEnd); System.out.println("ads:" + ads); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
测试:
import java.net.MalformedURLException; import com.hy.loong.unit.BaiDuMapUnit; public class Test1 { public static void main(String[] args) throws MalformedURLException { // TODO Auto-generated method stub BaiDuMapUnit.getGeocoderLatitude("广东省深圳市福田区奥林匹克大厦26"); BaiDuMapUnit.getPosition("22.556585037727996", "114.05420498035405"); } }
相关文章推荐
- 百度地图地址经纬度互转,java代码(v1版本使用)
- 百度地图经纬度和地址互转(Java代码)
- java后台百度地图经纬度和地址之间的相互转换(通过经纬度获取地址、通过地址获取经纬度)
- 百度地图 java 根据地址获取经纬度
- java 根据经纬度获取地址实现代码
- 地址到经纬度坐标转化的JAVA代码
- java 根据经纬度获取百度地图地址
- GMap.Net根据地址搜索,并双击获得经纬度((Winform)最新版本的代码)
- java通过百度地图把实际地址转换为经纬度
- GMap.Net根据地址搜索,并双击获得经纬度((Winform)最新版本的代码))
- java通过百度地图把实际地址转换为经纬度
- 利用GoogleApi根据经纬度查询地名,和根据地名查询经纬度Java代码
- Java初学者:图书管理小工具(MySQL版本)代码
- 百度地图 根据经纬度获取地址
- 百度地图根据经纬度获取地址
- Web墨卡托坐标与WGS84经纬度互转 java代码
- 利用GoogleApi根据经纬度查询地名,和根据地名查询经纬度Java代码
- 根据wsdl文件或者wsdl地址生成对应的java代码
- 计算 GPS 经纬度 两点间 距离 JAVA 代码
- Sonar 1.11版本发布,Java代码质量管理平台