我正在使用https://github.com/allenhwkim/angularjs-google-maps我的项目中的指令
以下是在 map 上绘制标记的代码示例
<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 的谷歌地图标记对象吗?
最佳答案
您可以使用 Angular 的范围来获取 Marker
对象。我没有看到你的 JS 代码,而是来自 Angularjs-Google-Maps example每个标记都可以通过 $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);
};
};
});
关于javascript - Ngmap 从 javascript 选择 ng-repeat 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32817510/