2011-04-29 52 views
11

我需要允許旅行者使用谷歌地圖來繪製路線,然後查詢我感興趣的點(例如,麥當勞的位置)的數據庫,然後顯示所有這些位置在他們將要採用的一英里或兩英里之內。問題是,如何有效地從谷歌(基本上是經緯度/長對數組)返回「駕車路線」信息,然後將其轉換爲SQL查詢以獲取距離路線一定距離內的位置?在Google地圖上做「沿路線的興趣點」

它不一定非常精確,而且「像鳥一樣飛翔」,距離路線的距離都很好。我最關心的是它是否合理高效。

在數據庫中,基本上每個條目都具有經度和緯度,但我可以根據需要更改數據庫架構。

作爲一個例子,這個網站做什麼,我想要做的(如果你給一個起點和終點,它會顯示在附近的公路上,你將採取雪佛龍站): http://www.chevron.com/products/stations/stationfinder/planyourroute.aspx

http://karmatics.com/docs/locationsalongroute.png

回答

12
+0

完美,正是我一直在尋找。 – rob 2011-04-30 00:15:00

+0

我很遺憾我只有一個投票權。 – Hemlock 2013-03-17 13:10:05

+0

我一直在尋找這個答案!謝謝! – ajbraus 2014-03-18 20:55:24

0
<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <title>Polygon arrays</title> 
    <style> 
     #map { 
     height: 100%; 
     } 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
    </style> 
    </head> 

    <body> 
    <div id="map"></div> 
    <script>  
var map; 
     function initMap() { 
     map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: 19.2570, lng: 72.8712}, 
      zoom: 10, 
     }); 

     var directionService = new google.maps.DirectionsService(); 
     var directionsRenderer = new google.maps.DirectionsRenderer({ map: map }); 
     var request = { 
      origin: "<?php echo $source;?>", 
      destination: "<?php echo $destination;?>", 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
     } 


     directionService.route(request, function(result, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 

      var path = result.routes[0].overview_path; 

      var poly = new google.maps.Polyline({ 
       strokeColor: '#FF0000', 
       strokeOpacity: 1.0, 
       strokeWeight: 3, 
      map: map, 
      }); 
      poly.setPath(path); 


      var myTollLocations = [ 
       <?php 

       isset($hello); //$hello is array which comes from database 
       foreach ($hello as $key => $value) { 
       ?> 
        new google.maps.LatLng(<?php echo $hello[$key]->latitude;?>, <?php echo $hello[$key]->longitude;?>), 
       <?php 
       } 
       ?> 

      ]; 


      for (var i = 0; i < myTollLocations.length ; i++) { 
       if (google.maps.geometry.poly.isLocationOnEdge(myTollLocations[i], poly,0.005)) { 
        console.log("found"); 


       }else{ 
        console.log("Not Found!"); 
       } 
      }; 




      <?php 
      $markersLocation = array($source, $destination); 
      foreach ($markersLocation as $key => $values) { 

      ?> 


      //Source Marker(convert address to LatLng for marker) 
      var geocoder = new google.maps.Geocoder();    
      geocoder.geocode({ 'address': '<?php echo $markersLocation[$key]?>'}, function(results, status) { 

       if (status == google.maps.GeocoderStatus.OK) { 
        var latitude = results[0].geometry.location.lat(); 
        var longitude = results[0].geometry.location.lng(); 
       } 

       console.log(latitude); 
       console.log(longitude); 

       var myLatLng = {lat: latitude, lng: longitude}; 



       var marker = new google.maps.Marker({ 
        position: myLatLng, 
        map: map, 
        title: 'source', 
        icon: 'http://innowrap.com/clients/digitoll/ic_red_marker.png' 

       }); 

      }); 


      <?php 
      } 
      ?> 


      } else { 
      alert("Directions query failed: " + status); 
      } 
     }); 


     } 


    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=ADD YOUR API KEY&libraries=geometry&callback=initMap" 
     async defer></script> 
    </body> 
</html> 
+0

雖然這個代碼片段是受歡迎的,並且可以提供一些幫助,但它會[如果它包括一個解釋](/ meta.stackexchange.com/q/114762)* how *和* why *會大大改進,這將解決問題。請記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 – 2017-02-10 12:54:00