如何使用angular-google-maps进行地理编码

4

我已经尝试了数小时,想要弄清楚如何使用谷歌的地理定位服务与Angular模块angular-google-maps。使用这个模块会让它变得很奇怪,不能像普通的谷歌地图JavaScript v3那样进行地理定位。

例如:

var geocoder = new google.maps.Geocoder();

谷歌未定义。

到目前为止,我已经解决了以下问题:

我的angular.module:

 ...'uiGmapgoogle-maps'])

 .config(['uiGmapGoogleMapApiProvider', function (GoogleMapApi) {
                GoogleMapApi.configure({
                  key: 'X',
                  v: '3.20',
                  libraries: 'weather,geometry,visualization' 
                });
              }]);

我的Angular控制器:

.controller(...., function (.., uiGmapGoogleMapApi, GoogleMapApi){

//Map setup stuff

 var geocoder = new GoogleMapApi.Geocoder();
        geocoder.geocode( { 'address': 'something'}, function(results, status) {
         if (status == GoogleMapApi.google.maps.GeocoderStatus.OK) {
           $scope.map.control.getGMap().setCenter(results[0].geometry.location);
           $scope.map.marker.latitude = results[0].geometry.location.lat();
           $scope.map.marker.longitude = results[0].geometry.location.lng();
         } else {
           alert('Geocode was not successful for the following reason: ' + status);
         }
        });

报错信息为Geocoder未定义。不确定该使用哪个对象来引用Geocoder(),是uiGmapGoogleMapApi还是GoogleMapApi?如何设置geocoder.geocode..? 请帮忙解决。

2个回答

9

我意识到只要使用HTTP请求即可避免所有问题:

 $http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + 
              address + '&key=X')
      .then(function(coord_results){
         $scope.queryResults = coord_results.data.results;
         $scope.geodata = $scope.queryResults[0].geometry;
       }, 
       function error(_error){
          $scope.queryError = _error;
      });

如果有人知道如何在不使用此方法的情况下解决上述问题,请仍然留言。


我正要回答同样的问题! - Ravimallya

2

我在做这件事情时也遇到了同样的问题:

  uiGmapGoogleMapApi.then(function(apiMaps) {
       var geocoder = new apiMaps.Geocoder();
       console.log(geocoder); //undefined
  });

我的解决方案:

  uiGmapGoogleMapApi.then(function(apiMaps) {
       var geocoder = new google.maps.Geocoder();
       console.log(geocoder); //object
  });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接