簡體   English   中英

在Ionic應用程序中,只有在刷新頁面時才會加載Google Maps,而從其他頁面返回時則不會加載,

[英]In an Ionic app, Google Maps only loads when page is refreshed, but not when coming from another page,

我試圖在Ionic應用程序中顯示Google Maps街景,但無法正常工作。 當我從另一個頁面轉到該頁面時,它不會加載。 但是,我刷新了當前頁面,然后街景起作用了。 關於如何解決此問題的任何想法?

controllers.js

.controller('StreetviewCtrl', function($scope, $ionicLoading, $state, $stateParams){
    console.log('Street view page loaded');

    Parse.initialize("xyz", "xyz");

  //Load the Goog map
    $scope.initialise = function() {

    console.log('initialize function');

    var myLatlng = new google.maps.LatLng(40.758774,  -73.98504);

        var mapOptions = {
            center: myLatlng,
            zoom: 1,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("street"), mapOptions);

        var panoramaOptions = {
            position: myLatlng,
            addressControl: false,
            pov: {
                heading: 34,
                pitch: 10
            }
        };

        var panorama = new google.maps.StreetViewPanorama(document.getElementById('street'), panoramaOptions);

      $scope.map=panorama;

    };

    google.maps.event.addDomListener(document.getElementById("street"), 'load', $scope.initialise());


    //Go to the guessing page
    $scope.makeGuess = function(item,event){
        console.log('guess button tapped');
        // $state.go('tab.account');
    };


})

模板(streetview.html)

<ion-view title="Where are you?">
  <ion-content class="padding">

    <center>
        <button on-touch="makeGuess()" class="button button-full button-positive">
            Make a Guess <i class="ion-arrow-right-a"></i>
        </button>
    </center>

    <div id="street" data-tap-disabled="false"></div>

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

要在每次進入視圖時重新觸發地圖加載,請執行以下操作(在控制器中):

        $scope.$on( "$ionicView.enter", function( scopes, states ) {
           google.maps.event.trigger( map, 'resize' );
        });

這對我有所幫助:在您離開“地圖”屏幕后,清除緩存(不要偽造將$ ionicHistory注入到控制器中),因此當您回來時-地圖將正確地重新呈現。

$ ionicHistory.clearCache();

這里得到

在狀態提供程序中添加此內容:cache:false(在定義地圖時)。 這對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM