angular-google-maps markers not showing up

2020-07-24 03:56发布

问题:

So I am using the angular-google-maps directive from http://angular-ui.github.io/angular-google-maps/#!/api and I am unable to see my markers on my map instance. I have tried just about everything and am desperate for advice. I have tried moving the $scope.markers to my $scope.map object, tried moving it inside my $watch function, and adding the values directly into the html with no luck. I am thinking this has something to do with the $scope.control.refresh() function i had to add in order for the map to fully load inside a tab (i am using angular-bootstrap tabs).

Thanks for any help!

HTML

   <div ng-controller="LocationCtrl" ng-cloak>
  <div class="map-canvas" ng-if="locationActive">
    <ui-gmap-google-map draggable="true" center='map.center' zoom='map.zoom' control="control">
      <ui-gmap-marker coords="markers.coords" idKey="markers.idKey">
      </ui-gmap-marker>
    </ui-gmap-google-map>
  </div>
</div>

JAVASCRIPT

(function() {
  'use strict';

  var locationCtrl = function($scope, uiGmapIsReady, $timeout) {
    $scope.map = {
      center: { latitude: 36.132411, longitude: -80.290481 },
      zoom: 15
    };

    $scope.control = {};

    $scope.active = $scope.$parent.active;
    $scope.locationActive = $scope.active().active;
    $scope.$watch($scope.active, function() {
      $timeout(function() {
        uiGmapIsReady.promise().then(function () {
          $scope.control.refresh();
          var map = $scope.control.getGMap();
          $scope.markers= {
            idKey: 1,
            coords: {
              latitude: 36.132411,
              longitude: -80.290481
            }
          };
        });
      }, 0);
    });
  };
  angular.module('boltlandApp').controller('LocationCtrl', locationCtrl);
})();

回答1:

Add in your HTML:

<ui-gmap-markers models="markers" coords="'coords'" idKey="'idKey'">
          </ui-gmap-markers>

Javascript:

Add this inside your $scope.$watch($scope.active, function() {

uiGmapIsReady.promise().then(function () {
         $scope.markers.push({
            idKey: 1,
              coords: {
                latitude: 36.132411,
                longitude: -80.290481
        },        
});


回答2:

Was fed up with angular-google-maps so i switched to ngMaps (angularjs-google-maps in github https://ngmap.github.io/maptypes.html#/maptype-image#maptype-image )

HTML

<div ng-controller="LocationCtrl" ng-cloak>
  <map class="map_canvas" ng-if="locationActive" ng-cloak center="32, -80" zoom="15">
    <marker position="32, -80" title="angularTestTitle"></marker>
  </map>
</div>

JAVASCRIPT

(function() {
  'use strict';

  var locationCtrl = function($scope, $timeout) {

    $scope.active = $scope.$parent.active;
    $scope.$watch($scope.active, function() {
      if($scope.active().title === "Location") {
      $scope.locationActive = true;
      } else {
        $scope.locationActive = false;
      }
      $timeout(function() {
      }, 0);
    });
  };
  angular.module('boltlandApp').controller('LocationCtrl', locationCtrl);
})();


回答3:

It doesn't work with ui-gmap-marker but works fine with ui-gmap-markers. I think it's because $scope.markers is not defined at first.

<!doctype html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.js"></script>
        <script src="https://raw.githubusercontent.com/lodash/lodash/2.4.1/dist/lodash.min.js"></script>
        <script src="https://raw.githubusercontent.com/angular-ui/angular-google-maps/master/dist/angular-google-maps.min.js"></script>
        <script src='//maps.googleapis.com/maps/api/js?sensor=false'></script>
        <style>
        .angular-google-map-container {
            width: 500px;
            height: 500px;
        }
        </style>
    </head>
    <body ng-app="boltlandApp">
        <div ng-controller="LocationCtrl">
          <div class="map-canvas">
            <ui-gmap-google-map draggable="true" center='map.center' zoom='map.zoom' control="control">
              <ui-gmap-markers models="markers" coords="'coords'" idKey="'idKey'">
              </ui-gmap-markers>
            </ui-gmap-google-map>
          </div>
        </div>
    </body>
    <script>
    
    (function() {
      'use strict';

      var locationCtrl = function($scope, uiGmapIsReady, $timeout) {
        $scope.markers = [];
        $scope.map = {
          center: { latitude: 36.132411, longitude: -80.290481 },
          zoom: 15
        };

        $scope.control = {};

        $scope.$watch($scope.active, function() {
          $timeout(function() {
            uiGmapIsReady.promise().then(function () {
              $scope.markers.push({
                idKey: 1,
                coords: {
                  latitude: 36.132411,
                  longitude: -80.290481
                },
                
              });
            });
          }, 0);
        });
      };
      angular.module('boltlandApp', ['uiGmapgoogle-maps'])
      angular.module('boltlandApp').controller('LocationCtrl', locationCtrl);
    })();
    </script>
</html>



回答4:

I have had this issue today and all that was required to fix it was to ensure you include the idKey



回答5:

Only 2 properties are required "idkey" and "coords" and both of them must be set in the markup

<ui-gmap-marker coords="marker.coords" idkey="marker.id">
                                                </ui-gmap-marker>

and please check spelling for each of them... Also if marker is added via some pure js (from event handler or something like this) necessary to digest a scope.

By the way you can ng-repeat markers

<ui-gmap-marker ng-repeat="m in map.markers" coords="m.coords" idkey="m.id" options="m.options" events="m.events">
                                                </ui-gmap-marker>