leaflet typescript turfjs

typescript - 使用Turf.nearPoint()时,应使用哪种点?

发布于 2020-04-07 11:45:21

我在这里放置的任何代码都是我必须从另一个系统抄录的,因此没有简单的方法可以发布代码。如果有的话,我会发布整个darn项目。

我想在我们的项目中实现此https://turfjs.org/docs/#nearestPoint,但我一直收到错误消息。

TS2345:“ FeatureCollection <几何| GeometryCollection,{[name:string]:任意;}不可分配给'FeatureCollection <Point,{[name:string]:any;}>'类型的参数

类型几何不能分配给点类型属性类型的类型不兼容类型字符串不能分配给点类型

我看到这个帖子turf.nearestPoint仅返回相同的点 在哪里在后有

map.on('click', function(e) {
  var coord = e.latlng;
  var lat = coord.lat;
  var lng = coord.lng;
  var targetPoint = turf.point([lng, lat]);
  var nearest = turf.nearestPoint(targetPoint, points);
    alert(JSON.stringify(nearest)); 
});

在我的代码中

  • 使用鼠标单击事件通过turf.point([lng,lat]}创建一个selectPoint

  • 从同样由turf.point([lng,lat])方法创建的点数组中创建点

但无济于事,我总是得到上面提到的TS错误。

此外,Turf页面上的文档还指出targetPoint是Coord,而其他张贴者代码具有Turf.point这是Feature

争论

参数------类型------说明

targetPoint ----坐标-----参考点

points ------ FeatureCollection <Point> ------反对输入点集

经过8个小时以上的尝试,我没有多余的头发可以拉扯,并且怀疑我今天所做的一切。

有人可以帮忙还是只给我指出一些实际起作用的东西,而没有仅针对已经熟悉该技术的人的神秘评论?

查看更多

提问者
Elijah
被浏览
42
Elijah 2020-02-13 21:44

只是为了帮助其他可能为此苦苦挣扎的人。

import * as Turf from '@turf/turf';

private handleClickEvent(e: Leaflet.LeafletEvent) {
    const latLng: Leaflet.LatLng = e.latlng;
    // Note in Turf it is backwards longitude, latitude where normally its lat, lng
    let mapClickLocation: Feature<Point> = Turf.point( [latLng.lng, latLng.lat] );

    let allYourGeoPoints: Array<Feature<Point>> = [];
    // Values you'd set in the allYourGeoPoints e.g.
    allYourGeoPoints.push(Turf.point([lng, lat], {'id': idOfYourRecord});

    // Create a collection of points usable by Turf
    let collectionOfPoints: FeatureCollection<Point> = Turf.featureCollection(allYourGeoPoints);

    // Now find the nearest item
    let nearestPointFound: NearestPoint = Turf.nearestPoint(mapClickLocation,  collectionOfPoints);

}