Spirit书生 2020-12-09 12:01 采纳率: 0%
浏览 168

高德api地址转经纬度问题

            $this.parmet.street.map((item, i) => {
                geocoder.getLocation(item.Address, function (status, result) {
                    if (status === 'complete' && result.geocodes.length) {
                        var lnglat = result.geocodes[0].location
                        if (item.Customer_Group_Class === '一阶') var style_ = 0;
                        if (item.Customer_Group_Class === '二阶') var style_ = 1;
                        if (item.Customer_Group_Class === '三阶') var style_ = 2;
                        var obj = {
                            "lnglat": [lnglat.lng, lnglat.lat],
                            "name": item.Customer_Name,
                            "style": style_,
                            node: item,
                        }
                        addData.push(obj);
                        resolve(addData);
                    } else {
                        console.log('根据地址查询位置失败:', item.Customer_Name);
                    }
                });
            })

通过数组遍历,将数组元素中的地址转为经纬度在高德地图上打点,但是‘geocoder.getLocation’方法需要异步解析,所以在循环外部拿到的 arrData 数组为空,产生了异步问题,该如何解决,还望指点一二,不胜感激

  • 写回答

2条回答 默认 最新

  • 玄机凭测 2020-12-09 13:01
    关注

    getLocation返回的是一个promise对象吗

    评论

报告相同问题?