2017-12-14 11 views
1

エフェクト内のポリライン上の位置の残りの距離を計算します。 Google Maps Projection(fromLatLngToPoint)に依存するSnapToPolylineクラスがあり、最も近い点を計算して距離を返します。問題は、地図が開かれたときにファクトリメソッドがその場で作成するNavigatorMap Dependencyです。ngrx実行時依存性を伴う効果

実行時に現在アクティブなNavigatorMapオブジェクトをコンテナから解決できますか?

どうすれば解決できますか?とにかくこれをエフェクトで解決できますか、コントローラからSetDistanceを起動するだけですか?

@Injectable() 
export class DistanceEffect{ 

    constructor(private actions$: Actions, 
       private store$: Store<AppState>, 
       private injector: Injector 
    ){} 

    @Effect() 
    updateDistance$ = this.actions$.ofType(SET_DIRECTION) 
     .withLatestFrom(this.store$) 
     .switchMap(([action, state]) => { 
      const navigatorMap = this.injector.get(NavigatorMap); 

      const direction: Direction = action.payload; 
      const coords = state.mapState.coords; 
      const snapToPolyline = new SnapToPolyline(navigatorMap, direction.polyline); 
      const distance = snapToPolyline.getRemainingDistAlongRoute(toLatLng(coords)); 

      return of(new SetDistance(distance)); 
     }); 
} 

答えて

0

は、私が直接投影機能を使用してSnapToPolylineからGoogleのライブラリの依存関係を削除

を解決しました。誰かが投影源と数式を探している場合。

// https://gis.stackexchange.com/questions/66247/what-is-the-formula-for-calculating-world-coordinates-for-a-given-latlng-in-goog 
//http://wiki.openstreetmap.org/wiki/EPSG:3857 
export function fromLatLngToPoint(latlng: google.maps.LatLng) { 
    const x = (latlng.lng() + 180)/360 * 256; 
    const y = ((1 - Math.log(Math.tan(latlng.lat() * Math.PI/180) + 1/Math.cos(latlng.lat() * Math.PI/180))/Math.PI)/2 * Math.pow(2, 0)) * 256; 
    return new google.maps.Point(x, y); 
} 

export function fromPointToLatLng(point: google.maps.Point){ 
    const lng = point.x/256 * 360 - 180; 
    const n = Math.PI - 2 * Math.PI * point.y/256; 
    const lat = (180/Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))); 
    return new google.maps.LatLng(lat, lng); 
} 
関連する問題