【问题标题】:NgMap error "could not find map"NgMap 错误“找不到地图”
【发布时间】:2016-05-13 15:25:06
【问题描述】:

我是 Angular 的新手,我正在尝试使用它来集成谷歌地图:https://github.com/allenhwkim/angularjs-google-maps

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

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

【问题讨论】:

标签: angularjs google-maps ng-map


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-08-21
    • 2016-11-25
    • 1970-01-01
    • 2018-03-02
    • 2015-07-27
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多