如何从地图控件的控制器中更新标记或添加新标记?
How to update markers or add new one from within the map-control's controller?
主要html:
<div ng-controller="MapCtrl as vm">
<ui-gmap-google-map center="vm.map.center" zoom="vm.map.zoom">
<ui-gmap-map-control template="mapControl.tpl.html" controller="MapControlCtrl"></ui-gmap-map-control>
<ui-gmap-marker idKey="vm.marker1.id" coords="vm.marker1.coords"></ui-gmap-marker>
</ui-gmap-google-map>
</div>
地图控制:
angularApp.controller('MapCtrl', function() {
var vm = this;
vm.map = {
zoom: 11,
center: {
latitude: ...,
longitude: ...
}
};
vm.marker1 = {
id: 1,
coords: {
latitude: ...,
longitude: ...
}
};
});
mapControl.tpl.html:
<button ng-click="addMarker()">Add</button>
<button ng-click="updateMarker1()">Update Marker 1</button>
MapControlCtrl:
angularApp.controller('MapControlCtrl', function($scope) {
$scope.addMarker = function() {
// how to add a marker to the map from within this method?
};
$scope.updateMarker1 = function() {
// how to update marker1 from within this method?
};
)};
您可能希望使用 <ui-gmap-markers>
来获取标记数组而不是 <ui-gmap-marker>
中的一个标记
然后您可以将新的标记对象推送到数据数组以在地图上显示
由于您在控制器中使用 "var vm = this" 语法,因此您必须使用 "controller as" 语法指定控制器。
ui-gmap-map-control template="mapControl.tpl.html"
controller="MapControlCtrl as vm"
请尝试上述更改。
主要html:
<div ng-controller="MapCtrl as vm">
<ui-gmap-google-map center="vm.map.center" zoom="vm.map.zoom">
<ui-gmap-map-control template="mapControl.tpl.html" controller="MapControlCtrl"></ui-gmap-map-control>
<ui-gmap-marker idKey="vm.marker1.id" coords="vm.marker1.coords"></ui-gmap-marker>
</ui-gmap-google-map>
</div>
地图控制:
angularApp.controller('MapCtrl', function() {
var vm = this;
vm.map = {
zoom: 11,
center: {
latitude: ...,
longitude: ...
}
};
vm.marker1 = {
id: 1,
coords: {
latitude: ...,
longitude: ...
}
};
});
mapControl.tpl.html:
<button ng-click="addMarker()">Add</button>
<button ng-click="updateMarker1()">Update Marker 1</button>
MapControlCtrl:
angularApp.controller('MapControlCtrl', function($scope) {
$scope.addMarker = function() {
// how to add a marker to the map from within this method?
};
$scope.updateMarker1 = function() {
// how to update marker1 from within this method?
};
)};
您可能希望使用 <ui-gmap-markers>
来获取标记数组而不是 <ui-gmap-marker>
然后您可以将新的标记对象推送到数据数组以在地图上显示
由于您在控制器中使用 "var vm = this" 语法,因此您必须使用 "controller as" 语法指定控制器。
ui-gmap-map-control template="mapControl.tpl.html"
controller="MapControlCtrl as vm"
请尝试上述更改。