JelleP
JelleP

Reputation: 1004

Google Maps Api check if place is on the route

I made with Google Maps an route between two places. Thats works fine.

But i also have an database with interesting points on different roads in my country. I like to show them if they are on the generated route. Places who are not one this route, don't need to be shown.

My database with intereseting points contains latitude and longitude coordinates.

How can i check is they are on my route? There are approximately 30 or 40 interesting point in my database.

// set request
var request = {
    origin: initialLocation,
    destination: destination,               
    travelMode: google.maps.TravelMode.DRIVING
};

 // make route
directionsService.route(request, function(response, status) {

    if (status == google.maps.DirectionsStatus.OK) {

      // set route
      directionsDisplay.setDirections(response);


    }

});         

UPDATE: Build new function "isLocationOnEdge". But the check runs the markers when they are not on the road:

// get flitsers on the route
function getFlitsersOnRoute(){

    // set request
    var request = {
        origin: current_location,
        destination: $('#gegevens').html(),               
        travelMode: google.maps.TravelMode.DRIVING
    };  

    // make route
    directionsService.route(request, function(response, status) {

        // isLocationOnEdge
        var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;      
        var coords = response.routes[0].overview_path;
        var image = base_url+'external/afbeeldingen/google_markers/flitser.png';

        // get flitsers
        $.ajax({
            type: "POST",
            async:false,
            url: base_url+"advies/get/9",
            success: function (data) {
                var result = jQuery.parseJSON(data);    

                // loop trough flitsers
                $.each(result.flitsers.m, function(i, item) {

                    // latitude longitude
                    var latlng = item["@attributes"].gps.split(",");

                    // make google latlng
                    var myLatlng = new google.maps.LatLng(latlng[0],latlng[1]);                     

                    if (myLatlng,coords)
                    {
                        // set and define the marker 
                        var marker = new google.maps.Marker({
                            position: myLatlng,
                            map: map,
                            size: new google.maps.Size(15, 15),
                            icon: image,
                            title: 'Flitser'
                        });     
                    }

                });
            }
        });

    });
}

Upvotes: 3

Views: 7735

Answers (2)

Felix Kirathe
Felix Kirathe

Reputation: 41

isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number)

To determine whether a point falls on or near a polyline, or on or near the edge of a polygon, pass the point, the polyline/polygon, and optionally a tolerance value in degrees to google.maps.geometry.poly.isLocationOnEdge(). The function returns true if the distance between the point and the closest point on the line or edge falls within the specified tolerance. The default tolerance value is 10-9 degrees.

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

Upvotes: 3

Tyler Eich
Tyler Eich

Reputation: 4248

Include the geometry library in your Google Maps API request:

http://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry&sensor=TRUE_OR_FALSE

Pseudo-code:

// Make isLocationOnEdge easier to access
var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;

for (var i = 0; i < interestingPlaces.length; i++) {
    if (isLocationOnEdge(interestingPlaces[i],
                         response.routes[0].overview_path))
        {
            // Do something with interestingPlaces[i]
        }
}

Google maps documentation for isLocationOnEdge()

Upvotes: 1

Related Questions