【问题标题】:Adding marker on map: AngularJS在地图上添加标记:AngularJS
【发布时间】:2015-02-16 21:57:55
【问题描述】:

我很难应付 AngularJS 及其设置地图和标记的方式。

现在我有以下代码:

<map data-ng-model="mymapdetail" zoom="11" 
              center="{{item.coordinates}}" style="heigth:375px">
<div data-ng-model="mymarker"></div>
</map>

这正确地显示了一个居中的地图。然后我尝试以这种方式添加标记。

$scope.mymarker = new google.maps.Marker({
            map: $scope.mymapdetail,
            position: new google.maps.LatLng(item.coordinates),
            title: woa.title
        });

此代码不会产生任何结果。哪里错了?

【问题讨论】:

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


【解决方案1】:

这是working solution,标记出现在点击和this one,标记已经可见-使用您的代码(几乎-我没有使用map指令),但您应该能够非常适应您的代码小的变化。 可能它在您的情况下不起作用的主要原因是使用new google.maps.LatLng(item.coordinates)

代码如下:

//Angular App Module and Controller
angular.module('mapsApp', [])
.controller('MapCtrl', function ($scope) {
    var item = {
        coordinates: [40.6423926, -97.3981926]
    };

    var woa = {
        city: 'This is my marker. There are many like it but this one is mine.'
    };


    //set up map
    var mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(40.0000, -98.0000),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };

    $scope.mymapdetail = new google.maps.Map(document.getElementById('map'), mapOptions);

    //add marker
    $scope.mymarker = new google.maps.Marker({
        map: $scope.mymapdetail,
        animation: google.maps.Animation.DROP,
        position: new google.maps.LatLng(item.coordinates[0], item.coordinates[1]),
        title: woa.city
    });

});

让我知道它是否适合您。

【讨论】:

  • 抱歉,我忘了说 'new google.maps.LatLng(item.coordinates)' 不起作用,因为 item.coordinates 是一个数组,而 google.maps.LatLnt 需要两个整数.
猜你喜欢
  • 1970-01-01
  • 2014-09-22
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 2018-05-26
  • 1970-01-01
相关资源
最近更新 更多