2017-01-01 46 views
1

我想獲得地理定位數據爲coords在DynamoDB存儲在我的離子應用,如果我按照我下面的代碼,我不斷收到一個錯誤,正在獲取地理位置數據存儲在離子2應用

bookResource(resourceId, timeslot): Promise<void> { 
let promise: Promise<void> = new Promise<void>((resolve, reject) => { 
    this.globals.displayLoader("Place..."); 

    let place : Place = { 
    coords: google.maps.LatLng(position.coords.latitude, position.coords.longitude), 
    userId: this.globals.getUserId(), 
    userFirstName: this.globals.getUserFirstName(), 
    userLastName: this.globals.getUserLastName(), 
    }; 
    this.authClient.getClient().placesCreate(this.globals.getUserId(), place).subscribe(
    (data) => { 
     place.placeId = data.placeId; 
     this.places.push(place); 
     resolve(); 
    }, 
    (err) => { 
     reject(err); 
    } 
); 
}); 
return promise; 
} 

coords上述代碼中的值不存儲地理位置座標,而我的userId值正確存儲,我該如何解決這個問題。謝謝

+0

你想獲得設備的當前位置? – raj

+0

@raj是的,我正在嘗試獲取設備的當前位置 –

回答

0

ionic的原生功能通常返回一個PromiseObservable,您只能在success回調內訪問。因此,如果您嘗試保存設備的位置,則可以使用離子型插件GeoLocation。退房docs

import { Geolocation } from 'ionic-native'; 
    getLocation(){ 
    let options = { maximumAge: 3000, timeout: 50000, enableHighAccuracy: true }; 
    return Geolocation.getCurrentPosition(options).then((pos)=>{ 
    let place : Place = { 
     coords: pos.coords, 
     userId: this.globals.getUserId(), 
     userFirstName: this.globals.getUserFirstName(), 
     userLastName: this.globals.getUserLastName(), 
    }; 
    return place; 
    }).catch((err) => { 
     console.log(err); 
    }); 
    } 

,那麼你可以像

this.getLocation().then((place)=>{ 
    console.log(place) // you can do whatever you want with ``place`` here 
    this.authClient.getClient().placesCreate(this.globals.getUserId(), place).subscribe(
    (data) => { 
     place.placeId = data.placeId; 
     this.places.push(place); 
    }, 
    (err) => { 
    } 
); 
}) 
+0

我已經更新了我的代碼,它返回了一個承諾,您是否可以修改我已經進入您的問題的代碼,我相信您的答案將起作用,謝謝 –

+0

我已更新我的答案用我認爲會爲你工作的解決方案。接受它作爲答案,如果它有幫助。 – raj

0

運行ngOnInit訪問的位置創建一個函數,它在數據庫

ngOnInit() { 
    this.map = this.initMap(); 
    setInterval(() => { 
    this.initMap(); 
    }); 
} 

下面是獲取經緯度值,並存儲其值要運行的函數OnInit返回承諾。

initMap(): Promise<void> { 
    let promise: Promise<void> = new Promise<void>((resolve, reject) => { 
     Geolocation.getCurrentPosition().then((position) => { 

     let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     let place: Place = { 
      coords: latLng, 
      userId: this.globals.getUserId(), 
      userFirstName: this.globals.getUserFirstName(), 
      userLastName: this.globals.getUserLastName(), 
     }; 
     let GooleMap = new google.maps.Map(document.getElementById('map'), { 
      center: latLng, 
      zoom: 18 
     }); 
      this.authClient.getClient().placesCreate(this.globals.getUserId(), place).subscribe(
      (data) => { 
       place.placeId = data.placeId; 
       this.places.push(place); 
       resolve(); 
      }, 
      (err) => { 
       reject(err); 
      }); 
     }); 
    }); 
    return promise; 
    }