wgs84转百度坐标代码:
//longi 经度 lati 纬度
public Map<String,String> convertLatitudeAndLongitude(String longi,String lati){
String urlPrefix = "http://api.map.baidu.com/geoconv/v1/?coords=";
String urlSuffixfix = "&from=1&to=5&ak=go8tg3oNKkk39pop9GTEMX6RaCGWdeN2";
Map<String,String> map = new HashMap<>();
try{
String url = urlPrefix+longi+","+lati+urlSuffixfix;
Map<String, String> headMap = new HashMap<>();
String result = HttpRequestUtils.httpGet(url);
if(null != result){
JSONObject jsonObject = JSONObject.parseObject(result);
Integer status = jsonObject.getInteger("status");
if(status == 0){
JSONArray jsonArray = jsonObject.getJSONArray("result");
JSONObject data = jsonArray.getJSONObject(0);
BigDecimal x = data.getBigDecimal("x");
BigDecimal y = data.getBigDecimal("y");
map.put("X",x.setScale(6,BigDecimal.ROUND_CEILING).toString());
map.put("Y",y.setScale(6,BigDecimal.ROUND_CEILING).toString());
}
}
return map;
}catch (Exception e){
e.printStackTrace();
return map;
}
}
wgs84转高德坐标代码:
public Map<String,String> convertLatitudeAndLongitudeToGaoDe(String longi,String lati){
String urlPrefix = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations=";
String urlSuffixfix = "&coordsys=gps&output=json&key=deaa723314d16df3bd1c9dcbcb3f7b56";
String returnResult = "高德坐标转换失败";
Map<String,String> map = new HashMap<>();
try{
String url = urlPrefix+longi+","+lati+urlSuffixfix;
Map<String, String> headMap = new HashMap<>();
String result = HttpClientPoolUtil.get(url, headMap);
if(null != result){
JSONObject jsonObject = JSONObject.parseObject(result);
Integer status = jsonObject.getInteger("status");
if(status == 1){
String locations = jsonObject.getString("locations");
String[] split = locations.split(",");
BigDecimal x = new BigDecimal(split[0]);
BigDecimal y = new BigDecimal(split[1]);
map.put("lati",y.setScale(6,BigDecimal.ROUND_CEILING).toString());
map.put("longi",x.setScale(6,BigDecimal.ROUND_CEILING).toString());
}
}
returnResult = "高德坐标转换成功";
return map;
}catch (Exception e){
e.printStackTrace();
return returnResult;
}
}
上面百度坐标转换用到的参数ak和高德坐标转换用到的参数key需要到对应地图开放平台去注册获取
百度开放平台地址:百度地图开放平台 | 百度地图API SDK | 地图开发
点击控制台进入应用管理页面,查看ak:
高德地图开放平台地址:高德开放平台 | 高德地图API
点击控制台进入应用管理页面,查看key: