使用angular-google-maps刷新地图和标记

时间:2015-03-19 15:31:58

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

您好我正在为我的项目使用angular-google-maps。 html和js代码如下

HTML:
     

        <ui-gmap-markers models="apartments" coords="'loc'" icon="'assets/images/map_icon_normal.png'" idkey="'_id'"
                     fit="'false'" click="click">


      <ui-gmap-windows show="'show'" >
        <div ng-non-bindable>{{streetAddress}}</div>

      </ui-gmap-windows>
    </ui-gmap-markers>



  </ui-gmap-google-map>

脚本: angular.module( '对myApp')

.controller('MapCtrl',
                    function ($scope, $routeParams, Map, uiGmapGoogleMapApi) {
 $scope.apartments = [];
 $scope.customIcon = "../../assets/images/map_icon_normal.png"

$scope.map = {
      center: {
        latitude: $routeParams.lat,
        longitude: $routeParams.lon
      },

      zoom: 5,
      bounds: {},
      events: {
        tilesloaded: function (map) {
          //console.log('tiles loaded');
          $scope.$apply(function () {
            $scope.mapInstance = map;
            //console.log(map.getBounds().getNorthEast());
            $scope.searchbox.options.bounds = new google.maps.LatLngBounds($scope.mapInstance.getBounds().getNorthEast(),
              $scope.mapInstance.getBounds().getSouthWest());

          });
        },
        idle: function(map) {

          $scope.map.refresh = false;

        },
        resize: function(map) {
          console.log('resize');
        },
        dragend: function() {

        }
      },
      markersEvents: {
        click: function(marker, eventName, model, args) {
          console.log('markerEvent click');
          $scope.map.window.model = model;
          $scope.map.window.show = true;
        }
      },

      window :  {
        marker: {},
        show: false,
        closeClick: function() {
          this.show = false;
        },
        options: {} // define when map is ready
      },

      control: {},
      refresh: function () {
        $scope.map.control.refresh();
      }
    }

    uiGmapGoogleMapApi.then(function (map) {

      $scope.getData(20, 0, map);

      map.visualRefresh = true;
      $scope.mapInstance = map;


    })

       $scope.getData = function(limit, offset, map) {
      Map.getApartments($routeParams.lat, $routeParams.lon, limit, offset).success(function (data) {
         ///----- I get undefined error here---
              $scope.map.control.refresh();



      });

    }})
}

我不确定如何使用新标记刷新地图,甚至不会触发对地图的更新。我玩过ui-gmap-google-map的'control'和'refresh'参数,但是无法让它工作。

2 个答案:

答案 0 :(得分:3)

您需要使用uiGmapIsReady ---而非uiGmapGoogleMapApi ---等待control个对象使用其方法进行扩充(例如refresh)。< / p>

uiGmapIsReady服务提供了一个承诺,该承诺在所有uiGmapGoogleMap指令完全初始化后解决,而uiGmapGoogleMapApi服务仅在加载Google Maps JS API时解析。

有关使用uiGmapIsReady的示例,请参阅this answer。当然,不要忘记the docs

关于如何更新示波器上的models属性以更新地图,您的代码示例需要充实并清理以帮助您。 (例如,getApartments更新$scope.apartments的解决方案是什么?或者可能会问这个问题本身就是答案?)看看this fiddle,它可能对你有所帮助其间。

答案 1 :(得分:1)

尝试dorebuildall属性。

<ui-gmap-markers 
    models="apartments"
    coords="'loc'"
    icon="'assets/images/map_icon_normal.png'"
    idkey="'_id'"
    fit="'false'"
    click="click"
    dorebuildall="true">
        <ui-gmap-windows></ui-gmap-windows>
</ui-gmap-markers>

更多详情请见:http://angular-ui.github.io/angular-google-maps/