在vue中进行reverseGeocoding

时间:2017-08-04 11:09:44

标签: javascript reverse-geocoding

我正在尝试使用vue.js和google api来反转地理编码...

我有lat lan值,但我真的希望使用lat和lan值来获取城市名称。我不知道如何....请帮助我并检查下面的代码

Vue.use(VueAxios, axios)

export default {
  name: 'Position',
  beforeCreate(){

    let googleApi = 'https://maps.googleapis.com/maps/api/geocode/json?latlng';
    let weatherApi = ''
    let token     = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
    let location  = '수원시 장안구 영화동';

    Vue.axios.get(googleApi, {
      params: {
        address: location,
        key: token
      }
    })
    .then(response => {
      console.log(response.data.results);
      console.log(response.data.results[0].address_components)
      let currentPosition = response.data.results[0].geometry.location;
      this.currentLatitude = currentPosition.lat;
      this.currentLongitude = currentPosition.lng;
      this.currentCity = response.data.results[0]
    })
    .catch(error => {
      console.log(error);
    });
  },

  data(){
    return {
          currentLatitude  : null,
          currentLongitude : null,
          currentCity : ''
           }
      },

1 个答案:

答案 0 :(得分:0)

我可以使用下面的代码在vue中做同样的事情。你可以尝试一下

axios.post('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + this.currentLocation.lat + ',' + this.currentLocation.lng + '&key=XXXXXXXXXXXXXXXXX')
        .then(response => {
        console.log(response.data);

        })
        .catch(e => {
        this.errors.push(e)
      })