2016-05-13 33 views
3

我是相當新的角度,我想使用這個集成谷歌地圖:https://github.com/allenhwkim/angularjs-google-mapsNgMap錯誤「找不到地圖」

我遇到的問題是,當我嘗試使用得到的地圖實例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); 
    }); 
}]); 
+0

無法重現錯誤。在[PLNKR](http://plnkr.co/edit/FbQKm1j7BUWRrWwsMdwi?p=preview)中運行良好。 – georgeawg

+0

對不起,我能夠在這裏重現它:http://plnkr.co/edit/N93h2rv6jW3yipze2ktX?p=preview –

回答

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 Provider API Reference

+0

這工作,謝謝。我覺得很愚蠢 –