I am trying to get the result from a function found in my provider to another page in my app. In this case the result is the "cityname". I get an undefined when console logging out the response.
location provider ts
getLocation(cityname) {
this.options = {
enableHighAccuracy: false
};
return this.geolocation.getCurrentPosition(this.options).then((pos: Geoposition) => {
var geocoder = new google.maps.Geocoder;
var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
var cityname = results[1].formatted_address;
return cityname;
} else {
console.log('No results found');
}
} else {
console.log('Geocoder failed due to: ' + status);
}
});
}, (err: PositionError) => {
console.log("error : " + err.message);
})
}
another page
import { LocationProvider } from './../../../../providers/location/location';
constructor(public navCtrl: NavController, public navParams: NavParams, public storage: Storage, private location: LocationProvider) {}
ionViewWillEnter() {
this.location.getLocation().then((data) =>{
console.log(data);
});
}
Any ideas what I'm doing wrong. The purpose is to get the city name to be re-used in other parts of my pp
Your code doesnt make sense as currently written :
cityname
in getLocation
implementation getLocation()
(in another page
) and getLocation(cityname)
(in location provider
) I suggest that you resolve your issues by cleaning up a bit :
location provider
getLocation() { // remove cityname from the signature, it is not used anyway
this.options = {
enableHighAccuracy: false
};
return this.geolocation.getCurrentPosition(this.options).then((pos: Geoposition) => {
var geocoder = new google.maps.Geocoder;
var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
return new Promise(function(resolve, reject) {
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[1]) {
var cityname = results[1].formatted_address;
resolve(cityname)
} else {
reject('No results found');
}
} else {
reject('Geocoder failed due to: ' + status);
}
});
})
}
您的代码中可能还存在其他问题,但是我注意到的第一件事是,在another page
您调用了不带参数的getLocation()
,而该函数被定义为使用cityname
作为参数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.