4

我对 Angular 很陌生,我正在尝试使用它来集成谷歌地图:https ://github.com/allenhwkim/angularjs-google-maps

我遇到的问题是当我尝试使用 NgMap.getMap() 获取地图实例时,当我 console.log NgMap.getMap 我得到“找不到地图”。这就是我的代码的样子:

部分/home.html:

<h1>practice locater</h1>
<ng-map id="map" center="[40.74, -74.18]"></ng-map>

Javascript:

var app = angular.module('RIThym',['ngResource','ngRoute','ngMap']);

app.config(['$routeProvider', function($routeProvider){
    $routeProvider
    .when('/', {
        templateUrl: 'partials/home.html',
        controller: 'HomeCtrl'
    })
    .otherwise({
        redirectTo: '/'
    });
}]);

app.controller('HomeCtrl',['$scope','$resource', 'NgMap',
function($scope, $resource, NgMap){
    var Locations = $resource('/api/locations');
    Locations.query(function(locations){
        $scope.locations = locations;
    });

    var map = NgMap.getMap();
    console.log(map);
    NgMap.getMap().then(function(map){
        console.log(map);
    });
}]);
4

1 回答 1

2

ng-map指令具有id属性时,getMap函数必须使用该id值作为其第一个参数。

HTML

<h1>practice locater</h1>
<ng-map id="map" center="[40.74, -74.18]"></ng-map>

JS

//DO this
NgMap.getMap("map").then(function(map){
//NOT this
//NgMap.getMap().then(function(map){
    console.log(map);
});

有关更多信息,请参阅NgMap 提供程序 API 参考

于 2016-05-18T21:44:53.983 回答