此坐标转换用的是GPSspg的API,如果您需要转几个的话还是在线转效率比较高;如果量比较大的话,转换不超过2000次/天,可以用此方法(免费);如果量很大的话,推荐订阅较高级的套餐。
单个在线转换:
地址:http://www.gpsspg.com/maps.htm
参数说明:
GPS 坐标 / WGS84,适用于谷歌地球 Google Earth。
Google 坐标,适用于 Google Maps 地图模式。
百度坐标,适用于百度地图。
高德坐标 / 腾讯坐标,适用于高德地图 / 腾讯地图。
Mapbar 图吧坐标,适用于图吧地图。
以下是批量转换:
serviceimpl层:
/** * 转换坐标百度坐标--->WGS84坐标系 */ @Override public Map<String, Object> baiduToGPS(Map<String, Object> mapParam) throws ServiceLocatorException { String convertUrl = "http://api.gpsspg.com/convert/coord/?oid=****&key=*****************&from=2&to=0&latlng="; try { Map map = new HashMap(); List<Map<String, Object>> DemoGPSlist = serviceDemo.selectDemo(map); for (Map<String, Object> BaiduGPSMap : DemoGPSlist) { String id = (String) BaiduGPSMap.get("ID"); String gisX = (String) BaiduGPSMap.get("GIS_X"); String gisY = (String) BaiduGPSMap.get("GIS_Y"); if (StringUtils.isNotBlank(gisY) && StringUtils.isNotBlank(gisX)) { String[] bzb = { gisY, gisX }; String url = convertUrl + StringUtils.join(bzb, ","); String bjson = JoddHttpUtils.postForm_1(url); if (StringUtils.isNotBlank(bjson)) { JSONObject bJSONObject = JSONObject.parseObject(bjson); int status = (int) bJSONObject.get("status"); if (status == 200) { JSONArray result = (JSONArray) bJSONObject.get("result"); if (result != null && result.size() > 0) { JSONObject zbObject = (JSONObject) result.get(0); String x = ""; String y = ""; if (zbObject.get("lng") != null) { x = zbObject.get("lng") + ""; } ; if (zbObject.get("lat") != null) { y = zbObject.get("lat") + ""; } ; if (!"null".equals(x) && !"null".equals(y) && StringUtils.isNotBlank(x) && StringUtils.isNotBlank(y)) { Map updateMap = new HashMap(); updateMap.put("ID", id); updateMap.put("GPSX84", x); updateMap.put("GPSY84", y); iDao.updateDemo(updateMap); } } } } } } } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); } return null; }
util类:
package com.demo.util; import java.util.Map; import jodd.http.HttpRequest; import jodd.http.HttpResponse; public class JoddHttpUtils { public static String postForm_1(String URL) { String json=""; try { HttpResponse response = HttpRequest .get(URL) .send(); if (response!=null) { json= response.bodyText(); } } catch (Exception e) { e.printStackTrace(); } return json; } }
serviceimpl层中的这句代码中的oid与key参考下图
http://api.gpsspg.com/convert/coord/?oid=****&key=*****************&from=2&to=0&latlng=
oid和key需要注册GPSspg账号才有,from=2&to=0参考以下说明:
API地址:http://www.gpsspg.com/api/convert/latlng/
测试类:
@Test public void baiduToGPS(){ try { Demo.baiduToGPS(null); } catch (Exception e) { e.printStackTrace(); } }