如何在地图控件的控制器内更新标记或添加新标记?

问题描述 投票:1回答:2

Main 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>

MapCtrl:

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?
  };
)};
javascript angularjs google-maps angular-google-maps
2个回答
1
投票

您想使用<ui-gmap-markers>而不是<ui-gmap-marker>中的一个标记数组)

然后您可以将新的标记对象推送到要在地图上显示的数据阵列中>


-1
投票

由于您在控制器中使用“ var vm = this”语法,因此必须使用“ controller as”语法指定控制器。

ui-gmap-map-control template="mapControl.tpl.html"
controller="MapControlCtrl as vm"
© www.soinside.com 2019 - 2024. All rights reserved.