2017-09-22 63 views
0

我在我的ionic2應用程序中正確使用google maps。我現在面臨的問題是如何將格式化地址保存到本地存儲。ionic2-將變量保存到本地存儲的錯誤

當我重新加載頁面我總是得到

Uncaught TypeError: Cannot read property 'set' of undefined 

下面是我的javascript

ionViewDidLoad(){ 


    let loader = this.LoadingController.create({ 
     content: 'Getting your Location' 
     }); 
    loader.present().then(()=>{ 
    this.geolocation.getCurrentPosition({ 
     enableHighAccuracy:true 

    }).then((resp) => { 
     //console.log(resp.coords.latitude) 
     //console.log(resp.coords.longitude) 
     this.lng = resp.coords.longitude; 
     this.lat = resp.coords.latitude; 
     this.storage.set('user_lng', this.lng); 
     this.storage.set('user_lat', this.lat); 

     var geocoder; 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(this.lat, this.lng); 

     geocoder.geocode(
      {'latLng': latlng}, 
      function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
         if (results[0]) { 
          this.address= results[0].formatted_address ; 
          //var value=add.split(","); 
          console.log("city name is: " + this.address); 
          this.storage.set('user_location', this.address); 
         } 
         else { 
          alert("address not found"); 
         } 
       } 
       else { 
        alert("Geocoder failed due to: " + status); 
       } 
      } 
    ); 

     let latLng = new google.maps.LatLng(this.lat,this.lng); 

      let mapOptions = { 
      center: latLng, 
      disableDefaultUI: true, 
      zoom: 11, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
      } 
      this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions); 

      this.http.get('assets/locations.json').map(res => res.json()).subscribe(data =>{ 
      //console.log(JSON.stringify(data)); 
      this.usermap= data; 

      let markers= this.usermap; 
      console.log(JSON.stringify(this.usermap)) 



      //this is to mark user current position on map 
      //var iconBase = 'assets/'; 

      var mypos_marker = new google.maps.Marker({ position: latLng, map: this.map,title:'My Position',animation: google.maps.Animation.DROP}); 
      mypos_marker.setMap(this.map); 
      this.myposinfo(mypos_marker); 

      //this is mark user's routes on map 
      for(let marker of markers) { 
      var position = new google.maps.LatLng(marker.latitude, marker.longitude); 
      var myroutes = new google.maps.Marker({position: position, title: marker.title}); 
      myroutes.setMap(this.map); 

      this.addInfoWindowToMarker(myroutes); 
      } 
     }); 
    }) 

    }) 
    loader.dismiss(); 
    } 

    addInfoWindowToMarker(marker) { 
    var infoWindowContent = marker.title; 
    var infoWindow = new google.maps.InfoWindow({ 
     content: infoWindowContent 
    }); 
    marker.addListener('click',() => { 
     infoWindow.open(this.map, marker); 
    }); 
    } 

    myposinfo(mypos_marker) { 
    var infoWindowContent = mypos_marker.title; 
    var infoWindow = new google.maps.InfoWindow({ 
     content: infoWindowContent 
    }); 
    mypos_marker.addListener('click',() => { 
     infoWindow.open(this.map, mypos_marker); 
    }); 
    } 

我不明白的是,上面的腳本this.storage.set('user_lng', this.lng);節省了用戶的緯度和經度的本地存儲正常工作

回答

0

確保您已導入IonicStorageModule進口部分

import { IonicStorageModule } from '@ionic/storage'; 


@NgModule({ 
    declarations: [ 

    ], 
    imports: [ 

    IonicStorageModule.forRoot() 
    ], 
    bootstrap: [IonicApp], 
    entryComponents: [ 

    ], 
    providers: [ 
    {provide: ErrorHandler, useClass: IonicErrorHandler} 
    ] 
}) 
+0

我已經做了,在app.module – user6579134

0

thisthis.storage,你把它是不正確的範圍,也不會包含屬性storage。你想要做的是保存在一個全局變量的存儲,在函數調用前:

import { IonicStorageModule } from '@ionic/storage'; 
    var storage = this.storage; 

    ionViewDidLoad(){ 
     .... 
     //now replace this.storage with the variable storage 
     .... 
    } 
+0

測試它,它不工作 – user6579134