Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Typescript获取当前位置并传递到后端以获取结果并传递到物料表数据源_Angular_Typescript - Fatal编程技术网

Angular Typescript获取当前位置并传递到后端以获取结果并传递到物料表数据源

Angular Typescript获取当前位置并传递到后端以获取结果并传递到物料表数据源,angular,typescript,Angular,Typescript,我的Angular 2项目试图获取用户的当前位置,因此在我的类中,我有以下代码: export class CurrentLocation { constructor(private http: Http) { } private lat : any; private lon : any; private params = new URLSearchParams(); private url = 'api/search/location'; getPosition

我的Angular 2项目试图获取用户的当前位置,因此在我的类中,我有以下代码:

export class CurrentLocation {
  constructor(private http: Http) { }
  private lat : any;
  private lon : any;
  private params  = new URLSearchParams();
  private url = 'api/search/location';


  getPosition = (lat, lon) => {
    navigator.geolocation.getCurrentPosition((position) => { 
      this.lat = position.coords.latitude; 
      this.lon = position.coords.longitude;
     });
  }
  getCurrentLocation(): Observable<any> {
    this.getPosition(this.lat, this.lon);
    console.log(this.lat+ "," + this.lon);
    //console.log(this.lat + ":" + this.lon);
    this.params.set('lat', this.lat);
    this.params.set('lon', this.lon);
    //console.log(this.params.toString());
    var result = this.http.get(this.url, { search: this.params });
    result.toPromise();
    return result;
  }
}
抱歉,代码有点混乱。。但我不知道如何将结果传递到
connect()
函数中,以使物料表工作


Github中的完整代码:

getCurrentLocation()中的所有代码都将在navigator.geolocation.getCurrentPosition的回调有机会运行之前运行。您可以通过在回调中放置另一个console.log语句来看到这一点:

navigator.geolocation.getCurrentPosition((position) => { 
  console.log("Got position", position.coords);
  this.lat = position.coords.latitude; 
  this.lon = position.coords.longitude;
});
getCurrentPosition需要使用回调有几个原因:

navigator.geolocation.getCurrentPosition((position) => { 
  console.log("Got position", position.coords);
  this.lat = position.coords.latitude; 
  this.lon = position.coords.longitude;
});
  • 地理位置查找可能需要浏览器查询一些在线服务以确定您的位置
  • 用户可能会收到提示,询问他们是否希望与网站共享其位置(这是大多数浏览器的默认行为)
解决方案可能涉及在getCurrentPosition周围制作一个包装器,以便返回一个承诺。我不知道角度2的观测值是如何工作的,所以我在这方面帮不了什么忙,但你可以让getPosition返回这样的承诺:

getPosition = () => {
  return new Promise((resolve, reject) => {
    navigator.geolocation.getCurrentPosition((position) => { 
      resolve(position.coords);
    }, (err) => {
      reject(err);
    });
  });
也许getCurrentLocation看起来像这样(只是猜测):

异步getCurrentLocation():PromiseLike{ 让coords=wait this.getPosition(this.lat,this.lon); this.lat=coords['lation']; this.lon=coords[‘经度’]; this.params.set('lat',this.lat); this.params.set('lon',this.lon); var result=this.http.get(this.url,{search:this.params}); 返回等待结果。toPromise(); }
对于
地理定位
要在移动浏览器上工作,请确保您通过
https
交付页面,否则移动设备将忽略您的位置请求。

可能存在重复的
async getCurrentLocation(): PromiseLike<any> {
  let coords = await this.getPosition(this.lat, this.lon);
this.lat = coords['latitude'];
this.lon = coords['longitude'];
  this.params.set('lat', this.lat);
  this.params.set('lon', this.lon);
  var result = this.http.get(this.url, { search: this.params });
  return await result.toPromise();
}