ionic2 app反向地理编码显示错误的结果

时间:2017-04-25 06:37:09

标签: angular typescript ionic2 reverse-geocoding

按一下按钮,我将填充带有位置地址的文本字段。

以下是获取当前位置然后从纬度和经度获取地址的代码。

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我正在获取当前位置的纬度和位置,即巴基斯坦卡拉奇。

1 个答案:

答案 0 :(得分:0)

尝试这个我认为问题在于你发送给地理编码器的latlng,

    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.');
                            }
                        })

         }