高德地图API通过地址 反查 省市区 和 经纬度 js源码和java源码
js
let amapKey;
// #ifndef MP
amapKey = ' ';
uni.request({
url: `https://restapi.amap.com/v3/geocode/geo?key=${amapKey}&address=${encodeURIComponent(
address,
)}`,
method: 'GET',
success: (res) => {
if ( res.data.status =='1' || res.statusCode == 200) {
const location = res.data.geocodes[0].location;
state.model.latitude = location.split(',')[0];
state.model.longitude = location.split(',')[1];
state.model.splat = location.split(',')[0];
state.model.splon = location.split(',')[1];
} else {
console.error('解析地址失败:', res.data);
}
},
fail: (error) => {
console.error('请求高德API失败:', error);
},
});
package com.scm.scx.framework.common.util.map;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.scm.scx.framework.common.util.http.HttpUtils;
/**
* 高德地图工具类
*/
public class GeoMapUtils {
/**
* 高德地图地址解析
* @param key
* @param address 详细地址
* @return
*/
public static JSONObject getGeoAddress(String key,String address){
JSONObject geocodes = new JSONObject();
String Url = "https://restapi.amap.com/v3/geocode/geo?key="+key+"&address="+address;
String responseStr = HttpUtils.post(Url,null,"");
JSONObject jsonObject = JSONUtil.parseObj(responseStr);
if (jsonObject != null && "1".equals(jsonObject.getStr("status"))) {
geocodes = jsonObject.getJSONArray("geocodes").getJSONObject(0);
String location = geocodes.getStr("location");
String[] lngAndLat = location.split(",");
double longitude = Double.parseDouble(lngAndLat[0]);
double latitude = Double.parseDouble(lngAndLat[1]);
geocodes.set("longitude",longitude);
geocodes.set("latitude",latitude);
geocodes.set("status",jsonObject.getStr("status"));
geocodes.set("info",jsonObject.getStr("info"));
geocodes.set("infocode",jsonObject.getStr("infocode"));
}
else {
geocodes.set("status",jsonObject.getStr("status"));
geocodes.set("info",jsonObject.getStr("info"));
geocodes.set("infocode",jsonObject.getStr("infocode"));
// throw new RuntimeException("地址解析失败,请检查API Key和地址是否正确");
}
return geocodes;
}
}
java常用code 根据ID或者code 获取name 省市区获取-优快云博客
//todo 待优化
JSONObject response = GeoMapUtils.getGeoAddress(geoLbsKey,customer.getDetailAddress());
if(response.getStr("info").equals("OK"))
{
customer.setSplat(new BigDecimal(response.getStr("latitude")));
customer.setSplon(new BigDecimal(response.getStr("longitude")));
}else{
respVO.getFailureCustomerNames().put(importCustomer.getName(),
StrUtil.format(ErrorCodeConstants.CUSTOMER_SYNC_JWD_FAIL.getMsg(), importCustomer.getName()));
return;
}