【问题标题】:ReferenceError: google is not defined using ng-map GeoCoder serviceReferenceError:未使用 ng-map GeoCoder 服务定义 google
【发布时间】:2017-03-04 17:34:51
【问题描述】:

我在尝试为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);
});  

如sn-p所示,我已经成功使用了其他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

提前感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    GeoCoder 服务使用google.maps.Geocoder class,而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) {
                //...
          });
    
     });
    

    演示

    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;
    
    
            }]);
    <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>

    【讨论】:

    • 完美的 Vadim,它可以用 getMap 承诺封闭它。谢谢!
    • 这不再有效,因为需要 API 密钥。有人知道完整的独立示例吗?
    猜你喜欢
    • 2023-03-30
    • 2020-10-04
    • 2018-11-11
    • 2019-06-09
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    相关资源
    最近更新 更多