ReferenceError:使用ng-map GeoCoder服务

时间:2016-10-22 14:00:26

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

我在尝试使用ng-map library的地理编码器服务时出现以下错误:

angular.js:13642 ReferenceError: google is not defined at Object.t [as geocode] (http://localhost:6240/Scripts/ng-map.min.js:25:28246)

我将服务注入我的控制器

appModule.controller('MyController', ['$scope', 'NgMap', 'NavigatorGeolocation', 'GeoCoder', 
    function ($scope, NgMap, NavigatorGeolocation, GeoCoder) {
        GeoCoder.geocode({address: 'Avenida Calle 26 # 40-40, Bogotá'}).then(function (result) {
            //... do something with result
            console.log('RESULT GEOCODER: ', result);
        });
    }]);

我也用NgMap函数测试它得到相同的参考错误

NgMap.getGeoLocation('Avenida Calle 26 # 40-40, Bogotá').then(function (result) {
    console.log('GETGEOLOCATION: ', result);
}, function (error) {
    console.log('Error getting geolocation: ', error);
});  

如代码段所示,我已成功使用其他NgMap和NavigatorGeolocation服务。

以下是我的网页代码

<div map-lazy-load="https://maps.google.com/maps/api/js" map-lazy-load-params="{{ $ctrl.googleMapsUrl }}">
    <div class="row">
        <div class="col-md-6">
            <ng-map id="map"
                center="{{ $ctrl.mapCenter }}"
                street-view="{{ $ctrl.svp }}"></ng-map>
        </div>
        <div class="col-md-6">
            <ng-map id="sv" />
        </div>
   </div>
</div>

并在相应的控制器/组件中

$ctrl.googleMapsUrl = "https://maps.googleapis.com/maps/api/js?key=MY-API-KEY";
$ctrl.mapCenter = 'current-location';

NavigatorGeolocation.getCurrentPosition({ timeout: 10000 }).then(function (ll) {
    $ctrl.svp = "StreetViewPanorama(document.querySelector('ng-map#sv'), {position:new google.maps.LatLng(" + ll.coords.latitude + " , " + ll.coords.longitude + ")})";
 }, function (error) {
     console.log('error getCurrentPosition: ', error);
 });

我正在使用AngularJS v1.5.6

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

GeoCoder服务使用google.maps.Geocoder class,后者又是Google Maps API的一部分。在控制器Google地图库中调用GeoCoder.geocode方法的时刻尚未加载(在您的情况下,它是通过map-lazy-load指令异步加载),这是出现此错误的原因。

您可以使用NgMap.getMap功能来保证Google Maps API准备就绪:

NgMap.getMap("map").then(function () {

      GeoCoder.geocode({ address: 'Avenida Calle 26 # 40-40, Bogotá' })
      .then(function (result) {
            //...
      });

 });

<强>演示

&#13;
&#13;
angular.module('mapApp', ['ngMap'])
    .controller('mapCtrl', ['$scope', 'NgMap', 'NavigatorGeolocation', 'GeoCoder',
        function ($scope, NgMap, NavigatorGeolocation, GeoCoder) {
            vm = this;

            NgMap.getMap("map").then(function () {

                GeoCoder.geocode({ address: 'Avenida Calle 26 # 40-40, Bogotá' })
                    .then(function (result) {
                        vm.mapCenter = result[0].geometry.location;
                    });

            });


            vm.googleMapsUrl = "https://maps.googleapis.com/maps/api/js";
            vm.mapCenter = null;


        }]);
&#13;
<script src="https://code.angularjs.org/1.5.6/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
<div ng-app="mapApp" ng-controller="mapCtrl as vm">

		<div map-lazy-load="https://maps.google.com/maps/api/js" map-lazy-load-params="{{ vm.googleMapsUrl }}">
			<ng-map id="map" center="{{ vm.mapCenter }}"></ng-map>
		</div>

</div>
&#13;
&#13;
&#13;