ngrx与运行时依赖关系的影响

时间:2017-12-14 11:28:06

标签: ngrx-effects

我想计算效果中折线上位置的剩余距离。 我有类SnapToPolyline,它依赖于Google Maps Projection(fromLatLngToPoint)来计算最近的点并返回距离。问题是NavigatorMap依赖关系,它是在打开地图时由工厂方法动态创建的。

我可以在运行时从容器中解析当前活动的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));
        });
}

1 个答案:

答案 0 :(得分:0)

解决

我直接使用Projection功能从SnapToPolyline中删除了Google Library的依赖关系。如果有人正在搜索投影源和公式。

// 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);
}
相关问题