从服务中获取地图操作员

时间:2018-07-12 12:58:20

标签: angular typescript oop rxjs ngrx

这是一个简单的概念,但是却笼罩着我。

getHoleLng(id: number | string) {
return this.getHoles().pipe(
  map(holes => holes.find(hole => hole.id === +id).lng)
  );
}

我正在尝试从服务中获取此方法,我想知道如何在我的地理位置组件中获取它?

Haversine(): void {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition( x => {

      this.myLat = x.coords.latitude;
      this.myLng = x.coords.longitude;

      this.courseLat = this._mont.getHoleLat();
      this.courseLng = this._mont.getHoleLng();


    console.log(`longitude: ${ this.courseLat } | latitude: ${ this.courseLng }`);
    console.log(`longitude: ${ this.myLat } | latitude: ${ this.myLng }`);


    const myCoords: GeoCoord = {
      latitude: this.myLat,
      longitude: this.myLng
    };

    const dominos: GeoCoord = {
      latitude: this.courseLat,
      longitude: this.courseLng
        // latitude: this.courseLat,
        // longitude: this.courseLng
    };

    this.metres = this._haversine.getDistanceInMeters(myCoords, dominos);
    this.yards = this._haversine.getDistanceInYards(myCoords, dominos);
    this.kilometres = this._haversine.getDistanceInKilometers(myCoords, dominos);
    this.miles = this._haversine.getDistanceInMiles(myCoords, dominos);

    this.metres = this.metres.toFixed(2);
    this.yards = this.yards.toFixed(2);
    this.kilometres = this.kilometres.toFixed(2);
    this.miles = this.miles.toFixed(2);
 });
}

}

修改

export class Hole {
  constructor(public id: number, public name: string, public lat: number, public lng: number) { }
}

const HOLES = [
   new Hole(1, 'Hole 1', 1234567, -12345324)
  ];

我正在尝试获取这些坐标!

1 个答案:

答案 0 :(得分:0)

您需要导入服务并将其注入到您的组件中。

在您的<name>.component.ts中:     从“ servicePath”导入{YourService};

export class NameComponent{
   // inject it here
   constructor(private yourService : YourService){
     // call it here (or elsewhere)
     this.yourService.getHoleLng(id).subscribe(...)
   }
}

或在您的函数中调用它:

Haversine(): void {
  // suppose for the example that id=1;
  let id = 1; // to be replaced by the wanted id

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition( x => {

      this.myLat = x.coords.latitude;
      this.myLng = x.coords.longitude;

      forkJoin(
         this.yourService.getHoleLng.pipe(map( lng=> courseLng=lng)),
         this.yourService.getHoleLat.pipe(map( lng=> courseLng=lat))
      ).subscribe( ()=> {
           console.log(`longitude: ${ this.courseLat } | latitude: ${ this.courseLng }`);
            console.log(`longitude: ${ this.myLat } | latitude: ${ this.myLng }`);

         const myCoords: GeoCoord = {
           latitude: this.myLat,
           longitude: this.myLng
         };

         const dominos: GeoCoord = {
           latitude: this.courseLat,
           longitude: this.courseLng
           // latitude: this.courseLat,
           // longitude: this.courseLng
         };

         this.metres = this._haversine.getDistanceInMeters(myCoords, dominos);
         this.yards = this._haversine.getDistanceInYards(myCoords, dominos);
         this.kilometres = this._haversine.getDistanceInKilometers(myCoords, dominos);
         this.miles = this._haversine.getDistanceInMiles(myCoords, dominos);

         this.metres = this.metres.toFixed(2);
         this.yards = this.yards.toFixed(2);
      })    

}

问题是,您需要先等待latlng坐标由服务返回,然后才能进行进一步处理。我使用forkJoin运算符来等待两个调用完成。