模态中的Google地图仅显示第一张地图

时间:2016-01-07 13:17:00

标签: javascript jquery google-maps ionic

我正在尝试使用google maps api来显示我从一组json数据传入的地址的地图,但是当我运行代码时,它只显示传递给它的第一个地址。打开模态时,它会显示地图,但一旦关闭并打开另一个地图,它就不会显示地图,只是一个空白区域。

模态控制器

.controller('ModalCtrl', function($scope, $ionicModal) {
  $ionicModal.fromTemplateUrl('templates/modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });
  $scope.openModal = function(b,c) {
    $scope.modal.show();
    var myLatlng = new google.maps.LatLng(b,c);
  var mapProp = {
    center: myLatlng,
    zoom:15,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("map"),mapProp);

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    });
    $scope.map = map;

  };
  $scope.closeModal = function() {
    $scope.modal.hide();
  };
  //Cleanup the modal when we're done with it!
  $scope.$on('$destroy', function() {
    $scope.modal.remove();
  });
  // Execute action on hide modal
  $scope.$on('modal.hidden', function() {
    // Execute action
  });
  // Execute action on remove modal
  $scope.$on('modal.removed', function() {
    // Execute action
  });

})

模态

<ion-modal-view>
    <ion-header-bar id="modalheader">
        <h1 class="title"></h1>
<!-- button to close the modal -->
        <button class="button icon ion-android-close" ng-click="closeModal();"></button>
    </ion-header-bar>
<ion-content class="item-icon-right item-text-wrap" id="modal">
<ion-list id="modalitem">
<ion-item>
<!-- displays the larger view of the office address -->
    <h1>{{office.LocationName}}</h1>
        <p id="mdets">{{office.LocAddressLine1 + ", " + office.LocAddressLine2 + ", " + office.LocCity + ", " + office.LocCountryDescription + ", " + office.LocZipPostalCode}}</p>
        <i ng-class="{'icon ion-android-star': favicon(office.id), 'icon ion-android-star-outline': !favicon(office.id)}"  ng-click="togglefav(office.id); $event.stopPropagation();"></i>
    </ion-item>
</ion-list>
<!-- creates the map view from the json data -->
<div data-tap-disable="true" draggable="false">
    <div id="map" style="width:100%; height:380px;"></div>
</div>

</ion-content>
</ion-modal-view>

可以打开模态的页面。

<ion-view view-title="Favourites">
<!-- template for the favourites page -->
    <ion-content>
        <ion-list>
        <!-- displays the items that the user has favourited through the filter function -->
        <ion-item id="fav" class="item-icon-right item-text-wrap" collection-repeat="office in offices|filter:ifinfav1" ng-controller="ModalCtrl" ng-click="openModal(office.Lat, office.Long); lastview(office.id);">
            <h3>{{office.LocationName}}</h3>
            <p id="details">{{office.LocAddressLine1 + ", " + office.LocAddressLine2 + ", " + office.LocCity + ", " + office.LocCountryDescription + ", " + office.LocZipPostalCode}}</p>
            <i ng-class="{'icon ion-android-star': favicon(office.id), 'icon ion-android-star-outline': !favicon(office.id)}" ng-click="togglefav(office.id); $event.stopPropagation();"></i>
        </ion-item>
        </ion-list>
    </ion-content>
</ion-view>

1 个答案:

答案 0 :(得分:0)

以下是在模式中使用Angular-google-maps的Ionic应用示例:

http://plnkr.co/edit/n4Qr3OMcGIb5uU28xISU?p=preview

在模态模板中有地图指令(ui-gmap-google-map,ui-gmap-marker,ui-gmap-marker):

    <div id="map_canvas">
      <ui-gmap-google-map center="map.center" zoom="map.zoom" options="map.options">
        <ui-gmap-marker idKey='1' coords='map.center'>
          <ui-gmap-window isIconVisibleOnClick="true">
                <div><a ng-click='clickTest()'>Click me!</a></div>
            </ui-gmap-window>
        </ui-gmap-marker>
      </ui-gmap-google-map>
    </div>