Search code examples
javascriptangularjsgoogle-mapsangular-google-maps

How to update markers or add new one from within the map-control's controller?


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?
  };
)};

Solution

  • You would want to use <ui-gmap-markers> that takes array of markers instead of one in <ui-gmap-marker>

    Then you can push new marker object to data array to be displayed on map