3

我在 Angular 中使用 ngMap 进行谷歌地图集成。

下面是我的html代码

<map ng-transclude class='google-map' center="map.center"> 
    <marker position="marker.position" options="marker.options"></marker>
</map>

我的控制器代码是

//response get from server side 
var onResponse = function(response){
    $scope.workshop = response;
    mapCenter(response.latitude, response.longitude);

};

function mapCenter(latitude, longitude) {

    $scope.map = {
      center: [latitude, longitude]
    };

    $scope.marker = {
      position: [latitude, longitude],
      options: function(){
        return {
          draggable: true
        }
      }
    };

};

在这种情况下,地图已显示,但地图中心未正确显示,它会显示在右侧。

如果我将下面的代码放在带有虚拟 lat 和 long 的函数之外,那么它工作正常,但我无法在这个函数之外获得动态 lat 和 long 值,这就是为什么我需要将此代码放入函数中的原因。

$scope.map = {
          center: [18.9750, 72.8258]
        };

请帮我解决。

4

2 回答 2

1

你可以检查一下

center="{{map.center}}"

$timeout并在函数中设置您的数据。如果您的纬度,经度完美,可能是角度消化问题

$timeout(function() {
    $scope.map = {
      center: [latitude, longitude]
    };
});
于 2016-02-04T11:51:16.063 回答
0

实际上你正在覆盖我们的$scope.map.

请更新

function mapCenter(latitude, longitude) {

                $scope.map.center =[latitude, longitude];
                $scope.marker.position = [latitude, longitude];
            };

也使用 dorebuildall="true"属性

于 2016-02-04T12:21:03.050 回答