Javascript TypeError:无法读取属性';PlacesService';未定义的

Javascript TypeError:无法读取属性';PlacesService';未定义的,javascript,google-maps,vue.js,google-places-api,Javascript,Google Maps,Vue.js,Google Places Api,我的目标是使用“RouterRequest”请求一组符合我标准的位置并保存它们。为了做到这一点,我使用谷歌的位置来检查我的地图中心周围的位置 let map = new google.maps.Map(document.querySelector("#myMap"), { center: { lat: 41.148481, lng: -8.606893 }, zoom: 15 }); let service = new google.maps.places.PlacesSe

我的目标是使用“RouterRequest”请求一组符合我标准的位置并保存它们。为了做到这一点,我使用谷歌的位置来检查我的地图中心周围的位置

let map = new google.maps.Map(document.querySelector("#myMap"), {
    center: { lat: 41.148481, lng: -8.606893 },
    zoom: 15
  });

let service = new google.maps.places.PlacesService(map);
service.nearbySearch(routeRequest, callback);


let routeRequest = {
    location: map.center,
    radius: '1000',
    type: [this.typeRoute]
  };


function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (const result of results) {
        this.listLocations.push(result);
      }
    }
  }

问题是,placesService似乎无法识别地图,因为您没有加载地图,所以出现了此错误。在
index.html
中,您有以下映射API脚本:

<script async src="https://maps.googleapis.com/maps/api/js?key="></script>

添加以下内容:

<script async src="https://maps.googleapis.com/maps/api/js?key=&libraries=places"></script>


希望这有帮助

在回调运行之前,您无法访问该属性。成功返回作业后,将作业移动到
if
检查中。@digitaldriver“作业”是什么意思?我是否应该将“service=new google.maps.places.PlacesService(map)”移动到回调函数中?如果我这样做,它会告诉我“服务”未定义,并且“google.maps.places.PlacesService(map)”已定义但未使用。请提供一个代码沙盒或stackblitz,以便我们可以从我们这边复制此问题?您的引导URL中是否加载了places库a la@evan这里是它的链接:。不过,您需要向它添加一个API密钥。使用谷歌地图API和谷歌地点API。