天地图高德地图根据经纬度反编译具体地址

393 阅读1分钟

需求

  • 在后台接口只返回经纬度,却无地址详细信息,而页面展示却要具体详细地址

天地图

// 通过经纬度获取当前地址信息  
getadress(longitude,latitude) {  
    let url = "http://api.tianditu.gov.cn/geocoder?type=geocode&tk=xxx";  
    // Lambda写法  
    const http = axios.create({  
        timeout: 60000  
    });  
    http.get(url, { params: { postStr: "{'lon': " + longitude + ",'lat': " + latitude + ",'ver': 1}" } }).then((response) => {  
    // 响应成功回调  
    let res = response.data;  
    if(res.result) {  
        this.$set(this.infoWindowData, 'address', res.result.formatted_address)  

    } else {  
        this.$message.error('获取具体地址失败')  
    }  
    });  
},

高德地图

// 通过经纬度获取当前地址信息  
getadress(longitude,latitude, marker) {  
    this.map.add(marker);  
    var geocoder = new AMap.Geocoder({  
        city: "010", //城市设为北京,默认:“全国”  
        radius: 1000 //范围,默认:500  
    });  
    const that = this  
    geocoder.getAddress([longitude,latitude], function(status, result) {  
        console.log(status, '查询经纬度');  
        if (status === 'complete'&&result.regeocode) {  
            that.$set(that.infoWindowData, 'address', result.regeocode.formattedAddress)  
        }else{  
        console.error('根据经纬度查询地址失败')  
        }  
    });  
},