简体   繁体   中英

What is the Ideal way to create a Triangle-Coordinates in Google Map API with two location points consisting of latitude & longitude

I have a scenario in my JavaScript application where I have the coordinates of a starting point which consist of Latitude and Longitude, similarly an ending point with it's respective coordinates.

Now I need to search for a location which basically provides with a set of coordinates and find if the recently entered location lies in between the previously mentioned starting point or ending point. However, the location does not need to match exactly within the points of the path of the start and end point. That is even if the location lies around the distance of say 2-3 km from the derived path, it should give a match.

I believe that we can create a triangle by providing three coordinates ie start-point, end-point and a third point. So once the triangle is formed we can use google.maps.geometry.poly.containsLocation method to find if our searched location is present inside this triangle.

So my question is how can we get a third point to create a triangle which will provide locations that are nearby within 2-3 km from start to end point.

Else is there any alternate approach to deal with my use case?

Use googlemap's geometry library

This function specifically isLocationOnEdge

Here's an example

0.001 tolerance value would be 100m

var isLocationNear = google.maps.geometry.poly.isLocationOnEdge(
 yourLatLng,
 new google.maps.Polyline({
   path: [
       new google.maps.LatLng(point1Lat, point1Long),
       new google.maps.LatLng(point2Lat, point2Long),
   ]
 }),
 .00001);

Please note that the following answer assumes Plane Geometry where you should be using Spherical Geometry instead. Although this will be fine for less accurate purposes (like approximate distance, etc..)

It seems more of a geometry question than a programming question. A triangle like you mentioned won't be able to cover a straight line path in a uniform way. The situation can be thought of more like a distance between point and a line problem (Refer the given diagram

图表

Here you can just find the distance between point C and line AB which you can check whether it's below 2.5 KMs (I've omitted all the units and conversions for simplicity)

点到线段的距离

Please note that you will also need to convert the distances from radian to appropriate units that you require using haversine formula, etc. which is not a trivial task ( https://www.movable-type.co.uk/scripts/latlong.html ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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