Javascript 从提供程序函数获取值到另一个页面

Javascript 从提供程序函数获取值到另一个页面,javascript,angular,typescript,ionic-framework,Javascript,Angular,Typescript,Ionic Framework,我正在尝试从我的提供者中找到的函数中获取结果,并将其发送到我的应用程序的另一个页面。在本例中,结果是cityname。当控制台注销响应时,我得到一个未定义的消息 位置提供程序ts 另一页 知道我做错了什么吗。其目的是让城市名称在我的pp的其他部分中重复使用。代码中可能存在其他问题,但我注意到的第一件事是,在另一个页面中,您调用getLocation时没有参数,而函数被定义为将cityname作为参数。您的代码在当前编写的代码中没有意义: 在getLocation实现中不使用变量cityname

我正在尝试从我的提供者中找到的函数中获取结果,并将其发送到我的应用程序的另一个页面。在本例中,结果是cityname。当控制台注销响应时,我得到一个未定义的消息

位置提供程序ts

另一页


知道我做错了什么吗。其目的是让城市名称在我的pp的其他部分中重复使用。代码中可能存在其他问题,但我注意到的第一件事是,在另一个页面中,您调用getLocation时没有参数,而函数被定义为将cityname作为参数。

您的代码在当前编写的代码中没有意义:

在getLocation实现中不使用变量cityname 另一页中的签名getLocation与位置提供程序中的getLocationcityname不匹配 GoogleMapsAPI允许您访问回调,而不是承诺。正如现在所写的,您的方法不会返回任何内容。您必须将回调转换为承诺,如中所述 我建议您通过清理一点来解决您的问题:

位置提供者


您正在调用getLocation,但签名是getLocationcityname。。这是一个输入错误吗?不是输入错误,如果我这样做,只会得到一个错误[ts]找不到名称'cityname'。如果我添加getLocationcityname,我会得到这个错误[ts]找不到名称'cityname'。我已经完成了上面的操作,并且用下面的内容更新了另一个页面,但是在控制台日志this.location.getLocation.thendata=>{console.logdata;};哦,等等,我没有注意到你的getLocation方法使用GeoCoder回调而不是promises。。。现在,函数不会返回任何内容,因为处理都是在回调内部完成的。将更新我的答案谢谢你这么多,这个作品,标记为正确答案
  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);
  }) 
  }
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);
       });

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