angular-google-maps标记未显示

时间:2015-01-23 15:52:08

标签: angularjs google-maps angular-google-maps

所以我使用的是angular-google-maps指令 http://angular-ui.github.io/angular-google-maps/#!/api 我无法在地图实例上看到我的标记。我已经尝试过几乎所有的东西,我非常渴望得到建议。我已经尝试将$ scope.markers移动到我的$ scope.map对象,尝试将其移动到我的$ watch函数中,并将值直接添加到html中,没有运气。我认为这与我必须添加的$ scope.control.refresh()函数有关,以便地图完全加载到选项卡内(我使用的是angular-bootstrap选项卡)。

感谢您的帮助!

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

5 个答案:

答案 0 :(得分:7)

添加HTML:

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

使用Javascript:

$scope.$watch($scope.active, function() {

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

答案 1 :(得分:2)

厌倦了角度谷歌地图,所以我切换到ngMaps(github https://ngmap.github.io/maptypes.html#/maptype-image#maptype-image中的angularjs-google-maps)

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

答案 2 :(得分:1)

它不适用于ui-gmap-marker,但可以与ui-gmap-markers一起使用。我认为这是因为$ scope.markers最初没有定义。

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

答案 3 :(得分:0)

我今天遇到过这个问题,解决这个问题所需要做的就是确保你加入idKey

答案 4 :(得分:0)

只需要2个属性&#34; idkey&#34;和&#34; coords&#34;并且必须在标记中设置它们

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

请检查每个人的拼写... 此外,如果通过一些纯粹的js(来自事件处理程序或类似的东西)添加标记来消化范围。

顺便说一句,你可以重复标记

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