2017-04-25 69 views
0

在按鈕上按下我正在使用位置地址填充文本字段。ionic2應用程序反向地理編碼顯示錯誤的結果

以下是我的代碼,獲取當前位置,然後從經度和緯度獲取地址。

useLocation(){ 
     let loc: any = {lat: 60.1676, lon: 24.9359}; 

getCurrentLocation().then((position: Geoposition) => { 
    let loca: any = {lat: position.coords.latitude, lon: position.coords.longitude}; 
      let req: GeocoderRequest = { position: loca } 
       Geocoder.geocode(req).then((results)=>{ 
         if (results.length) { 
          var result = results[0]; 
          var position = result.position; 
          var address = [ 
          result.subThoroughfare || "", 
          result.thoroughfare || "", 
          result.locality || "", 
          result.adminArea || "", 
          result.postalCode || "", 
          result.country || ""].join(", "); 
          this.profile.address = address; 
         } else { 
          alert("Not found"); 
         } 
       }) 

},() => { 
}); 
} 

上面的代碼返回阿爾及利亞某處的地址,而可變loca我得到緯度和經度我目前的位置是巴基斯坦卡拉奇。

回答

0

試試這個,我認爲問題是要發送到地理編碼的經緯度,

getCurrentLocation().then((position: Geoposition) => { 
      let loca: any = {lat: position.coords.latitude, lon: position.coords.longitude}, 
       geocoder = new google.maps.Geocoder(), 
       latlng = new google.maps.LatLng(loca.lat, loca.lng), 
    request = { 
     latLng: latlng 
     }; 
         geocoder.geocode(request, (results, status) => { 
          if (status == google.maps.GeocoderStatus.OK) { 
           if (results.length) { 
            var result = results[0]; 
            //do stuff here 
          } 
          else { 
           console.log('Cannot determine address of this location.'); 
          } 
         }) 

     } 
+0

[TS]提供的參數不匹配,通話對象的任何簽名。 此錯誤出現在geocoder.geocode(...)行 – Yawar

+0

執行此行後'latlng'中沒有值。 latlng = new google.maps.LatLng(loca.lat,loca.lng), – Yawar

+0

latlng中應該有值。你會得到loca.lat和loca.lng – RemyaJ