Leaflet.js - Draw polyline to nearest point from a location

前端 未结 1 1621
不知归路
不知归路 2021-02-10 18:10

I need to draw a polyline between two points using the shortest distance. Example, my location is New York, I have a location in China and I want to draw a polyline connecting t

1条回答
  •  野的像风
    2021-02-10 18:21

    Here's a working jsfiddle that answers the line problem. There is a method attached to the LatLng object called wrap that is supposed to help. I had to experiment a bit to get it work. I settled on

    if (Math.abs(loc1.lng - loc2.lng) > 180) {
      latlongs = [loc1.wrap(179, -179), loc2];
    }
    

    Unfortunately, as you can see, the marker for China isn't replicated. I added the worldCopyJump: true option to the map object, but it doesn't fix that particular problem. Not sure how to fix that.

    Edit: For posterity's sake, I forgot to mention that the reason it wasn't working before was because of the transition over the International Date Line.

    0 讨论(0)
提交回复
热议问题