// 地址获取经纬度(正编译)\n/**\n *\n * @param {String} address 地址\n * @param {Function} callback 回调函数\n * @param {Object} otherProps 其他参数\n */\nexport const getLngLatByAddress = (address, callback, otherProps) => {\n AMap.plugin('AMap.Geocoder', () => {\n const geocoder = new AMap.Geocoder({\n ...otherProps,\n })\n\n geocoder.getLocation(address, (status, result) => {\n console.log('222正编译result', result)\n callback(status, result)\n })\n })\n}\n如何调用这个方法及接收返回值内容:可以通过以下方式调用这个方法并接收返回值:\n\njavascript\ngetLngLatByAddress('北京市朝阳区', (status, result) => {\n if (status === 'complete' && result.info === 'OK') {\n const lnglat = result.geocodes[0].location\n console.log('经纬度:', lnglat)\n } else {\n console.log('获取经纬度失败')\n }\n})\n\n\n在上述代码中,我们调用getLngLatByAddress方法并传入地址参数北京市朝阳区,同时传入一个回调函数作为参数。当获取经纬度成功时,status为complete,result.info为OK,我们可以从result.geocodes[0].location中获取到经纬度。如果获取经纬度失败,status可能为其他值,我们可以在回调函数中处理失败的情况。


原文地址: https://www.cveoy.top/t/topic/pOnl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录