简体   繁体   English

Typescript中的LeafletJS多折线

[英]LeafletJS multi-polyline in Typescript

I'm trying to use the demo from http://leafletjs.com/reference-1.2.0.html#polyline 我正在尝试使用http://leafletjs.com/reference-1.2.0.html#polyline中的演示

// create a red polyline from an array of LatLng points
var latlngs = [
    [45.51, -122.68],
    [37.77, -122.43],
    [34.04, -118.2]
];
var polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
// zoom the map to the polyline
map.fitBounds(polyline.getBounds());

However, it's throwing me an error: 但是,这使我出错:

[ts]
Argument of type 'number[][]' is not assignable to parameter of type 'LatLngExpression[]'.
  Type 'number[]' is not assignable to type 'LatLngExpression'.
    Type 'number[]' is not assignable to type '[number, number]'.
      Property '0' is missing in type 'number[]'.

is something else needed when using leaflet with Angular/TypeScript? 在Angular / TypeScript中使用传单时,还需要其他东西吗?

Looks like your code is fine, but the types are a little finicky. 看起来您的代码不错,但是类型有些挑剔。 As the compiler notes, number[] is not assignable to [number, number] , which makes total sense (as an empty array [] should assign to number[] but not [number, number] ). 正如编译器指出的那样, number[]不可分配给[number, number] ,这是完全有意义的(因为空数组[]应该分配给number[]而不是[number, number] )。

Anyway, all you have to do is give typescript a hint that you are actually passing the right type. 无论如何,您要做的就是给打字稿提示您实际上正在传递正确的类型。 I would recommend you declare it on your variable, eg: 我建议您在变量中声明它,例如:

var latlngs: [number, number][] = [
    [45.51, -122.68],
    [37.77, -122.43],
    [34.04, -118.2]
];

Or, if in your actual use case that value comes from somewhere else, you can just cast it: 或者,如果在您的实际用例中该值来自其他地方,则可以将其强制转换为:

var polyline = L.polyline(latlngs as [number, number][], {color: 'red'}).addTo(map);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM