javascript - 模式中的谷歌地图仅显示第一张 map

标签 javascript jquery google-maps ionic-framework

我正在尝试使用谷歌地图 API 来显示我从一组 json 数据传入的地址的 map ,但是当我运行代码时,它只显示传入其中的第一个地址。当模式打开时,它会显示 map ,但一旦关闭并打开另一个模式,它根本不会显示 map ,而只是空白。

模态 Controller

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

最佳答案

这是一个使用 Angular-google-maps 的 Ionic 应用程序示例在模态中:

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

在模态模板中有 map 指令(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>

关于javascript - 模式中的谷歌地图仅显示第一张 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656144/

相关文章:

ios - 应用因使用Google Maps SDK被Apple拒绝

javascript - 具有不同对象的数组的indexOf

javascript - 如何从异步函数中提取输出-react-native?

c# - 如何在悬停时更改图像源?

php - 如何在 PHP 中使用 JQuery AJAX 调用触发 "error"回调

google-maps - 使用 geoxml3 集群 kml 文件

javascript - map 引擎 + 地理位置 Javascript API

javascript - 无法使用 Jquery 从 Api 接收 Json 响应

JavaScript:事件触发恢复innerHtml属性

javascript - 页面加载时动画 div 不透明度 jQuery