Ngmap从javascript

时间:2015-09-28 07:14:07

标签: jquery angularjs google-maps angularjs-ng-repeat ng-map

我在项目中使用https://github.com/allenhwkim/angularjs-google-maps指令

以下是在地图上绘制标记的代码示例

<map id="map" style="height:450px" zoom="4" zoom-to-include- markers='auto' center="{[{center.lat}]},{[{center.long}]}" on-center-changed="centerChanged()">
<marker id={[{$index}]} animation="DROP" ng-repeat="location in locations" position="{[{location.latitude}]},{[{location.longitude}]}" on-click="click()" title="Click to zoom"></marker> 

</map>

我现在必须从javascript函数中选择这些标记。 通过id选择标记会将标记作为dom元素,如下所示

<marker id="1" animation="DROP" ng-repeat="location in locations" position="10.0050407,76.3459498" on-click="click()" title="Click to zoom" class="ng-scope"></marker>

而不是谷歌地图标记对象

有没有我可以选择通过ng-repeat初始化的标记作为javascript的谷歌地图标记对象?

1 个答案:

答案 0 :(得分:3)

您可以使用angular的范围获取Marker对象。我没有看到您的JS代码,但$scope.map.markers[key] $scope.map.markers可以引用每个标记,var app=angular.module('myapp', ['ngMap']); app.controller('MarkerRemoveCtrl', function($scope) { $scope.positions = [{lat:37.7699298,lng:-122.4469157}]; $scope.addMarker = function(event) { var ll = event.latLng; $scope.positions.push({lat:ll.lat(), lng: ll.lng()}); } $scope.deleteMarkers = function() { $scope.positions = []; }; $scope.showMarkers = function() { for (var key in $scope.map.markers) { $scope.map.markers[key].setMap($scope.map); }; }; $scope.hideMarkers = function() { for (var key in $scope.map.markers) { $scope.map.markers[key].setMap(null); }; }; }); 其中ID TIMESTAMP BOARD DEVICE_ID[40] ERROR_CNT[40] TEMP[40] MORE_DATA[40]... 是您范围内的标记数组。

ID TIMESTAMP BOARD DEVICE_ID ERROR_CNT TEMP MORE_DATA
1  437892    1     1         100       25   xxx
1  437892    1     2         50        28   yyy