12

I am using Google Direction API to plot the route path between 2 places A and B. I am able to do this. Now, I have a requirement to check if given a place C falls in the route path of A and B.

Here is the snapshot of the route path that I have generated from my code.

Route map between A and B

Here is the corresponding code:

function initialize() {
                var input = document.getElementById('searchTextFieldSource');
                var input1 = document.getElementById('searchTextFieldDestination');

                var autocomplete = new google.maps.places.Autocomplete(input);
                var autocomplete1 = new google.maps.places.Autocomplete(input1);
                google.maps.event.addListener(autocomplete1, 'place_changed', function () {
                    var place = autocomplete.getPlace();
                    document.getElementById('city1').value = place.name;
                    var place1Lat = place.geometry.location.lat();
                    var place1Lng = place.geometry.location.lng();
                    document.getElementById('cityLat1').value = place1Lat;
                    document.getElementById('cityLng1').value = place1Lng;

                    var obj = new Object();
                    obj.city =place.name;
                    obj.latitude = place.geometry.location.lat();
                    obj.longitude = place.geometry.location.lng();
                    locations.push(obj);


                    var place2 = autocomplete1.getPlace();
                    document.getElementById('city2').value = place2.name;
                    var place2Lat = place2.geometry.location.lat();
                    var place2Lng = place2.geometry.location.lng();
                    document.getElementById('cityLat2').value = place2Lat;
                    document.getElementById('cityLng2').value = place2Lng;

                    var obj = new Object();
                    obj.city = place2.name;
                    obj.latitude = place2.geometry.location.lat();
                    obj.longitude = place2.geometry.location.lng();
                    locations.push(obj);

                    directionsDisplay = new google.maps.DirectionsRenderer();
                    var startPlace = new google.maps.LatLng(place1Lat, place1Lng);

                    var mapOptions = {
                        zoom:7,
                        center: startPlace
                    }

                    var map = new google.maps.Map(document.getElementById('map'), mapOptions);
                    directionsDisplay.setMap(map);
                    //refreshMap(locations);

                    var start = $("#city1").val();
                    var end = $("#city2").val();
                    var request = {
                          origin:start,
                          destination:end,
                          travelMode: google.maps.TravelMode.DRIVING
                    };
                    directionsService.route(request, function(response, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                          directionsDisplay.setDirections(response);
                        }
                    });
                });
            }

How can I go about it?

Adarsh Konchady
  • 2,577
  • 5
  • 30
  • 50
  • I don't see "place C" in your code. One option is to use RouteBoxer to determine a reasonable bounds for your route. If the intermediate point is contained by those bounds, that might be good enough, otherwise you can perform further tests (like distance from the route polyline. See [this similar question](http://stackoverflow.com/questions/20476917/find-a-place-lies-between-source-and-destination-google-maps-and-places-api) (which doesn't have an answer either) – geocodezip Dec 10 '13 at 17:58
  • 1
    [Gas Stations along (within ~0.25 miles of) your route](http://www.geocodezip.com/v3_SO_RouteBoxerPlaces_configurable.html?dist=0.25&to=Electronics%20City&from=BTM%20Layout&type=gas_station&name=&submit=) – geocodezip Dec 10 '13 at 19:13
  • 1
    @geocodezip - That's not quite the same. In this case, you already know the position of place C. RouteBoxer would be overkill for this problem. – Adam Jenkins Dec 11 '13 at 18:14
  • Yes. It would be computation intensive to use RouteBoxer for the case when C is known. – Adarsh Konchady Dec 11 '13 at 19:22

1 Answers1

12

You can use the geometry library that (you can request with google maps by changing your script src to https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry) and use isLocationOnEdge and use the LatLng of point C and the polyline that is returned from the DirectionsService.

https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge

Then again, point C could ALWAYS be on the way between A and B if you added it as a waypoint, so determining if point C is "on the way" is actually a bit of a tricky concept - how far out of the way is too far for it to not be "on the way"?

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • So using isLocationOnEdge, I can only find the points on the polyline right? Not the points which may be near the polyline(like say 0.5 kms from the polyline). – Adarsh Konchady Dec 11 '13 at 19:23
  • Look at the link to the documentation. `isLocationOnEdge` takes an optional third argument - `tolerance` which is the number of degrees you specify for the point to still be considered on the edge - i.e. for the function to return `true` It's worth pointing out that the same number of degrees will give you a different **real** distance (in km or mi) depending on the exact latitude or longitude. – Adam Jenkins Dec 11 '13 at 19:27
  • 1
    Any idea? how to do it with php and my sql. i am having a multidimensional array and each object inside it has {lat,lng}. i want to check if any one or more points from the multidimensional array are lying on the current route – Kamlesh Jha Jun 26 '18 at 05:54
  • @KamleshJha Did you completed your requirement work? I have the same problem.. plz reply. – SRB Bans Oct 29 '20 at 12:01