# Find a point in a polyline which is the closest to a given latlng

**URL:** <https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802>\
**Category:** CesiumJS\
**Created:** [March 3, 2016, 10:28pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802 "2016-03-03T22:28:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucenkom](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lucenkom](https://community.cesium.com/u/lucenkom)\
**Post date:** [March 3, 2016, 10:28pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/1 "2016-03-03T22:28:53Z")

</div>

Hi guys.

Can you help me please.

I have polyline points array [langitude(degrees), latitude(degrees), altitude(meters)] and I need to find the closest point in polyline array to a given point(latitude(degrees) longitude(degrees)).

How can I do it using Cesium API ?

Thank you very much.

---

<div class="post-metadata">

**Author:** ![hannah](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/hannah/32/4509_2.png) [@hannah](https://community.cesium.com/u/hannah)\
**Post date:** [March 4, 2016, 2:27pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/2 "2016-03-04T14:27:55Z")

</div>

Hello,

There isn’t a helper function in the Cesium API to do this, you will have to do your own calculation.

Best,

Hannah

---

<div class="post-metadata">

**Author:** ![lucenkom](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lucenkom](https://community.cesium.com/u/lucenkom)\
**Post date:** [March 5, 2016, 10:33pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/3 "2016-03-05T22:33:41Z")

</div>

Can you provide an algorithm to do it in Cesium please ?

---

<div class="post-metadata">

**Author:** ![Willem](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@Willem](https://community.cesium.com/u/Willem)\
**Post date:** [March 6, 2016, 12:33pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/4 "2016-03-06T12:33:27Z")

</div>

If you need distance over the curved ellipsoid, Cesium.EllipsoidGeodesic has the surfaceDistance property ([http://cesiumjs.org/Cesium/Build/Documentation/EllipsoidGeodesic.html](http://cesiumjs.org/Cesium/Build/Documentation/EllipsoidGeodesic.html)).  
If you do not (need to) care about the ellipsoid, you can transform (lon, lat, height) to cartesian and calculate the straight distance ([http://cesiumjs.org/Cesium/Build/Documentation/Cartesian3.html](http://cesiumjs.org/Cesium/Build/Documentation/Cartesian3.html)).

Regards, Willem

---

<div class="post-metadata">

**Author:** ![lu44anno](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@lu44anno](https://community.cesium.com/u/lu44anno)\
**Post date:** [March 6, 2016, 1:49pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/5 "2016-03-06T13:49:41Z")

</div>

Another question:

How can I find a PROJECTION of the given latlng point on the polyline in Cesium 3D view?

Thank you all.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Kennedy](https://avatars.discourse-cdn.com/v4/letter/n/ac8455/32.png) [@Nicholas\_Kennedy](https://community.cesium.com/u/Nicholas_Kennedy)\
**Post date:** [March 7, 2016, 8:21pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/6 "2016-03-07T20:21:45Z")

</div>

Correct me if I am wrong, but there is no “projection” in the classic sense in 3D mode. Lat/Lon points are displayed on a model of the Ellipsoid. Cesium uses WGS84 by default. This is configurable to a user defined Ellipsoid.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Kennedy](https://avatars.discourse-cdn.com/v4/letter/n/ac8455/32.png) [@Nicholas\_Kennedy](https://community.cesium.com/u/Nicholas_Kennedy)\
**Post date:** [March 7, 2016, 8:23pm UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/7 "2016-03-07T20:23:23Z")

</div>

> Correct me if I am wrong, but there is no “projection” in the classic sense in 3D mode. Lat/Lon points are displayed on a model of the Ellipsoid. Cesium uses the WGS84 by default. This is configurable to a user defined Ellipsoid. If you pick a point and you are using the default Ellipsoid the crs for that point would be 4326.

> 

> > Another question:  
> > How can I find a PROJECTION of the given latlng point on the polyline in Cesium 3D view?

> > Thank you all.

---

<div class="post-metadata">

**Author:** ![lu44anno](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@lu44anno](https://community.cesium.com/u/lu44anno)\
**Post date:** [March 8, 2016, 8:21am UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/8 "2016-03-08T08:21:56Z")

</div>

Can you help me to find a point in a polyline which is the closest to a given latlng in the most effiecient way ?

---

<div class="post-metadata">

**Author:** ![Nicholas\_Kennedy](https://avatars.discourse-cdn.com/v4/letter/n/ac8455/32.png) [@Nicholas\_Kennedy](https://community.cesium.com/u/Nicholas_Kennedy)\
**Post date:** [March 8, 2016, 11:16am UTC](https://community.cesium.com/t/find-a-point-in-a-polyline-which-is-the-closest-to-a-given-latlng/3802/9 "2016-03-08T11:16:10Z")

</div>

Test for the bouding box of the polygon first, then use the winding number algorithm, cache your vertices if you are testing many points.
