我在下图中解释了我的问题(对不起,我说英语不太好)。
我使用Google Maps Directions API来计算起点和终点之间的路线,并且它们之间有一个位置(您在地图中看到)。我怎么知道这个地方在路上?
答案 0 :(得分:1)
The only way I can think of (and I have used this method) is thus:
When you make a directions request, you receive a DirectionsResult object. If you drill down into that result you can get latitudes and longitudes. If you are using the JavaScript API, for example:
// Every step is about 22 LatLngs per mile; we'll try one LatLng per 3
// miles or so:
var specRoute = directionResult.routes[0].legs[0].steps[i];
for (var j = 0; j < specRoute.path.length; j = j + 171) {
var lattotal = latarr.push(specRoute.path[j].lat());
var lngtotal = lngarr.push(specRoute.path[j].lng());
}
In this example I am formatting arrays of lats and lngs to be compared to a lot of other lats and lngs in a php file elsewhere. It sounds like you only need to check one latlng so it should be simpler.
What I did thereafter (and what you could do) is to determine how close to the path your point is required to be, and write some logic to check. You could do that with straight latitude and longitude numbers, or convert to another distance measurement (miles, meters, etc).
There is a negative aspect in that you will have to deal with converging longitude lines the farther north you go (in the Northern hemisphere). I used a regression fit workaround but there is probably a better way.
There may also be a better method for doing this, but this is the only way I could think of on past projects.