在Vuejs创建的方法中未定义Google地图对象

时间:2017-11-08 17:15:43

标签: javascript vuejs2 vue-component

created () {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(position => {
      console.log(this.position = position.coords)
      var geocoder = new google.maps.Geocoder()  //ERROR IN THIS LINE 
      var googleLatLon = { lat: position.croods.latitude, lng: position.croods.longitude }
      geocoder.geocode({ 'location': googleLatLon }, (results, error) => {
        console.log(results[0].address_components[2].long_name + ', ' + results[0].address_components[3].long_name + ', ' + results[0].address_components[4].long_name + ', ' + results[0].address_components[5].long_name)
      })
    })
  }
}

的index.html

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="http://maps.google.com/maps/api/js"></script>
<link rel="manifest" href="<%= htmlWebpackPlugin.files.publicPath %>static/manifest.json">

我在index.html中添加了google的地图api,上面的代码来自App.vue。我哪里出错了?

1 个答案:

答案 0 :(得分:0)

您正在编写错误的变量名称 position.croods。 它应该是position.coords

中没有错误
var geocoder = new google.maps.Geocoder()