最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps Api check if place is on the route - Stack Overflow

programmeradmin2浏览0评论

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'
                        });     
                    }

                });
            }
        });

    });
}

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'
                        });     
                    }

                });
            }
        });

    });
}
Share Improve this question edited Jan 10, 2014 at 7:01 JelleP asked Jan 7, 2014 at 15:05 JellePJelleP 1,0047 gold badges21 silver badges40 bronze badges 5
  • Add the interesting places to an array. Then for each coord in the route polyline, check it against the array of interesting places. – Andy Commented Jan 7, 2014 at 15:13
  • possible duplicate of How to to Get Places (e.g Gas Stations) along Route Between Origin and Destination in Google Maps API, use your database instead of the places API. – geocodezip Commented Jan 7, 2014 at 15:39
  • Thanks for ments! The answer below is an example of your answers i guess. – JelleP Commented Jan 8, 2014 at 7:00
  • Where are you using isLocationOnEdge? How does your program know when to place a marker on the line? – Tyler Eich Commented Jan 10, 2014 at 21:06
  • new google.maps.Marker creates the marker. The var is only to store the marker in a later stadium. – JelleP Commented Jan 13, 2014 at 6:56
Add a ment  | 

2 Answers 2

Reset to default 3

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./maps/documentation/javascript/geometry

Include the geometry library in your Google Maps API request:

http://maps.googleapis./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()

发布评论

评论列表(0)

  1. 暂无评论