【问题标题】:In Google Angular Map api, how to get places details using place_id?在 Google Angular Map api 中,如何使用 place_id 获取地点详细信息?
【发布时间】:2026-02-08 00:25:01
【问题描述】:

我想使用 place_id 获取地点信息like this example 位置、城市、州、经度、纬度、formatted_address。我收到错误 Uncaught TypeError: undefined is not a function。我正在使用this: places/place-id google api 获取地址。 var service = new google.maps.places.PlacesService(map); 内部有使用 map 变量因为我使用的是 angular-google-map,所以我将 $scope.map 作为参数而不是 map 传递。请检查以下代码,如果我错了,我是因为在 PlacesService 中传递了 $scope.map 而我是错误的,它期望 map 作为 this example 中的参数。

HTML

<ui-gmap-google-map center='map.center' zoom='map.zoom' control='map.control' id="map-canvas">
  <ui-gmap-markers models="markers" idkey="markers.id" coords="'coords'" >

  </ui-gmap-markers>
</ui-gmap-google-map>

控制器

 var map={
       center: {
         latitude: 
         longitude: 
       },
       bounds : ,
       zoom: 5,
       control : {}
    };
    $scope.map = map;

 var service = new google.maps.places.PlacesService($scope.map);
 service.getDetails({ placeId: $scope.place_id}, function(place, status){
          console.log('place='+place);
 });

【问题讨论】:

  • 您从哪里得到“未定义不是函数”?您可以粘贴错误跟踪吗?
  • @ZhenyangHua 由于我在 PlacesService 中使用 $scope.map,我得到了定义............
  • PlacesService(attrContainer:HTMLDivElement | Map),$scope.map 只是一个对象字面量,它不是 Map 类型或 DOM。要获取Map 类型的对象,请使用$scope.map.control.getMap() 并将其解析为PlacesService(map) 方法。
  • @ZhenyangHua 谢谢回复,你说的像var map=$scope.map.control.getMap() then place inside PlacesService(map)
  • @ZhenyangHua 如果您在回答部分给出解释,我将不胜感激......

标签: javascript angularjs google-maps google-maps-api-3


【解决方案1】:

基于上面的 cmets,var map=$scope.map.control.getGMap() 是解决方案。

所以对于其他使用 angular-google-map 的人,你需要使用$scope.map.control.getGMap() 来获取当前的地图实例。

【讨论】: