高德地图升级为2.0问题汇总
标记点数据获取
1.Uncaught TypeError: Cannot read properties of undefined (reading ‘extData’)
marker.on("click", function (e) {
//var d = e.target.Qi.extData; 旧版本使用方式
var d = e.target.getExtData(); //需要使用这种方式才能取到值
});
插件注册方式
旧版本注册插件的方式
AMap.plugin(["AMap.Autocomplete", "AMap.Geocoder"], function () {
geocoder = new AMap.Geocoder({
city: locationInfo.Citycode,
batch: true,
});
auto = new AMap.Autocomplete({
input: "tipInput",
city:locationInfo.Citycode,
citylimit: "这里传需要的值",
});
AMap.event.addListener(auto, "select", function (d) {
if (d.poi.location == undefined) {
return;
}
mapObj.setZoomAndCenter(18, [d.poi.location.lng, d.poi.location.lat]);
mapObj.clearMap();
marker = new AMap.Marker({
map: mapObj,
draggable: true,
position: [d.poi.location.lng, d.poi.location.lat]
});
marker.on("dragend", function (data) {
getAddress(data.lnglat);
});
getAddress([d.poi.location.lng, d.poi.location.lat]);
});
新版本
1.首先引入loader.js
<script src="https://webapi.amap.com/loader.js"></script>
window._AMapSecurityConfig = {
securityJsCode: "高德地图key",
};
AMapLoader.load({
key: "高德地图key值", //申请好的 Web 端开发 Key,首次调用 load 时必填
version: "2.0", //指定要加载的 JS API 的版本,缺省时默认为 1.4.15
plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.PlaceSearch", "AMap.AutoComplete", "AMap.Geocoder"], //需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['AMap.Scale','...','...']
AMapUI: {
//是否加载 AMapUI,缺省不加载
version: "1.1", //AMapUI 版本
plugins: ["overlay/SimpleMarker"], //需要加载的 AMapUI ui 插件
},
Loca: {
//是否加载 Loca, 缺省不加载
version: "2.0", //Loca 版本
},
})
.then((AMap) => {
//这里进行地图的初始化
})
.catch((e) => {
console.error(e); //加载错误提示
});