2016-03-26 4 views
-1

オフィスアドレスのマーカーポイントを含むウェブマップが埋め込まれています。しかし、Webブラウザのジオロケーションを使って、現在の場所から私のオフィスの場所までの道順をユーザーに提示する必要があります。出来ますか?埋め込みのGoogleマップで訪問者の現在地から自分の場所に向かう方法

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="UTF-8" /> 
    <title>Find a route using Geolocation and Google Maps API</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=true"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
    <script> 
     function calculateRoute(from, to) { 
     // Center initialized to Naples, Italy 
     var myOptions = { 
      zoom: 10, 
      center: new google.maps.LatLng(40.84, 14.25), 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     // Draw the map 
     var mapObject = new google.maps.Map(document.getElementById("map"), myOptions); 

     var directionsService = new google.maps.DirectionsService(); 
     var directionsRequest = { 
      origin: from, 
      destination: to, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING, 
      unitSystem: google.maps.UnitSystem.METRIC 
     }; 
     directionsService.route(
      directionsRequest, 
      function(response, status) 
      { 
      if (status == google.maps.DirectionsStatus.OK) 
      { 
       new google.maps.DirectionsRenderer({ 
       map: mapObject, 
       directions: response 
       }); 
      } 
      else 
       $("#error").append("Unable to retrieve your route<br />"); 
      } 
     ); 
     } 

     $(document).ready(function() { 
     // If the browser supports the Geolocation API 
     if (typeof navigator.geolocation == "undefined") { 
      $("#error").text("Your browser doesn't support the Geolocation API"); 
      return; 
     } 

     $("#from-link, #to-link").click(function(event) { 
      event.preventDefault(); 
      var addressId = this.id.substring(0, this.id.indexOf("-")); 

      navigator.geolocation.getCurrentPosition(function(position) { 
      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({ 
       "location": new google.maps.LatLng(position.coords.latitude, position.coords.longitude) 
      }, 
      function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) 
       $("#" + addressId).val(results[0].formatted_address); 
       else 
       $("#error").append("Unable to retrieve your address<br />"); 
      }); 
      }, 
      function(positionError){ 
      $("#error").append("Error: " + positionError.message + "<br />"); 
      }, 
      { 
      enableHighAccuracy: true, 
      timeout: 10 * 1000 // 10 seconds 
      }); 
     }); 

     $("#calculate-route").submit(function(event) { 
      event.preventDefault(); 
      calculateRoute($("#from").val(), "govandi")); 
     }); 
     }); 
    </script> 
    <style type="text/css"> 
     #map { 
     width: 500px; 
     height: 400px; 
     margin-top: 10px; 
     } 
    </style> 
    </head> 
    <body> 
    <h1>Calculate your route</h1> 
    <form id="calculate-route" name="calculate-route" action="#" method="get"> 
     <label for="from">From:</label> 
     <input type="text" id="from" name="from" required="required" placeholder="An address" size="30" /> 
     <a id="from-link" href="#">Get my position</a> 
     <br /> 

     <label for="to">To:</label> 
     <input type="text" id="to" name="to" placeholder="Another address" size="30" /> 
     <a id="to-link" href="#">Get my position</a> 
     <br /> 

     <input type="submit" /> 
     <input type="reset" /> 
    </form> 
    <div id="map"></div> 
    <p id="error"></p> 
    </body> 
</html> 
+1

それは可能ですか?ええ、すべて可能です。最大の問題は、あなたがそれに費やしたい時間はどれくらいですか?これまでに何を試みましたか? – SZenC

+0

@SZenCチュートリアルに目を通しましたが、そのほとんどはジオロケーションまたは事前定義されたルートを有効にする方法を示しましたが、結合されていませんでした。 –

+0

あなたはこれらの2つを組み合わせることはできませんか? – SZenC

答えて

0
<!DOCTYPE html> 
<html> 

<head> 
    <meta charset="UTF-8" /> 
    <title>Find a route using Geolocation and Google Maps API</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=true"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
    <script> 

     function calculateRoute(from, to) { 
      var myLatLng = {lat: 19.056, lng: 72.921}; 

      // Center initialized to Mumbai, India 
      var myOptions = { 
       zoom: 10, 
       center: myLatLng, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 

      // Draw the map 
      var mapObject = new google.maps.Map(document.getElementById("map"), myOptions); 





      var directionsService = new google.maps.DirectionsService(); 
      var directionsRequest = { 
       origin: from, 
       destination: to, 
       travelMode: google.maps.DirectionsTravelMode.DRIVING, 
       unitSystem: google.maps.UnitSystem.METRIC 
      }; 
      directionsService.route(
       directionsRequest, 
       function (response, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         new google.maps.DirectionsRenderer({ 
          map: mapObject, 
          directions: response 
         }); 

         $('.distance-in-km').text(response.routes[0].legs[0].distance.value/1000 + "km"); 

         alert(response.routes[0].legs[0].distance.value/1000 + "km"); // the distance in metres 
        } else 
         $("#error").append("Unable to retrieve your route<br />"); 
       } 
      ); 
     } 

     $(document).ready(function() { 
      // If the browser supports the Geolocation API 
      if (typeof navigator.geolocation == "undefined") { 
       $("#error").text("Your browser doesn't support the Geolocation API"); 
       return; 
      } 

      navigator.geolocation.getCurrentPosition(function (position) { 
        var geocoder = new google.maps.Geocoder(); 
        geocoder.geocode({ 
          "location": new google.maps.LatLng(position.coords.latitude, position.coords.longitude) 
         }, 
         function (results, status) { 
          if (status == google.maps.GeocoderStatus.OK) { 
           calculateRoute(results[0].formatted_address, "govandi"); 
          } else { 
           var marker = new google.maps.Marker({ 
            position: myLatLng, 
            title: 'Hello World!' 
            }); 

           marker.setMap(mapObject); 

           $("#error").append("Unable to retrieve your address<br />"); 
          } 
         }); 
       }); 



       calculateRoute($("#from").val(), "govandi"); 

      $("#calculate-route").submit(function (event) { 
       event.preventDefault(); 



       calculateRoute($("#from").val(), "govandi"); 
      }); 


      $('.verify-location > a').click(function(){ 
       $('.verify-location').hide(); 
       $('#calculate-route').show(); 
      }); 
     }); 
    </script> 
    <style type="text/css"> 
     #map { 
      width: 500px; 
      height: 400px; 
      margin-top: 10px; 
     } 
     #calculate-route { 
      display: none; 
     } 
     .verify-location > a { 
      cursor: pointer; 
      color: #FCA2A2; 
     } 
    </style> 
</head> 

<body> 
    <div class="verify-location">Is the your location incorrect? <a>Click here to enter your location manually</a></div> 
    <form id="calculate-route" name="calculate-route" action="#" method="get"> 
     <label for="from">From:</label> 
     <input type="text" id="from" name="from" placeholder="An address" size="30" /> 
     <button type="submit">Submit</button> 
    </form> 
    <div id="map"></div> 
    <p id="error"></p> 
    <p class="distance-in-km"></p> 
</body> 

</html> 
1

私は少し選択を使用して、使用して-地理位置-と-グーグルマップ-APIを/目的地を設定するには、ユーザーの場所からの旅のプランナーを持っているhttps://www.sitepoint.com/find-a-route-yからのコードを修正しましたHTML形式。

誰にでも役立つ場合があります。

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8" /> 
<title>Find a route using Geolocation and Google Maps API</title> 
<script src="http://maps.google.com/maps/api/js?sensor=true"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 


<script> 
    function calculateRoute(from, to) { 
    // Center initialized somewhere near London 
    var myOptions = { 
     zoom: 10, 
     center: new google.maps.LatLng(53, -1), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    // Draw the map 
    var mapObject = new google.maps.Map(document.getElementById("map"), myOptions); 

    var directionsService = new google.maps.DirectionsService(); 
    var directionsRequest = { 
     origin: from, 
     destination: to, 
     travelMode: google.maps.DirectionsTravelMode.TRANSIT, 
     unitSystem: google.maps.UnitSystem.METRIC 
    }; 
    directionsService.route(
     directionsRequest, 
     function(response, status) 
     { 
     if (status == google.maps.DirectionsStatus.OK) 
     { 
      new google.maps.DirectionsRenderer({ 
      map: mapObject, 
      directions: response 
      }); 
     } 
     else 
      $("#error").append("Unable to retrieve your route<br />"); 
     } 
    ); 
    } 

    $(document).ready(function() { 
    // If the browser supports the Geolocation API 
    if (typeof navigator.geolocation == "undefined") { 
     $("#error").text("Your browser doesn't support the Geolocation API"); 
     return; 
    } 

    $("#from-link, #to-link").click(function(event) { 
     event.preventDefault(); 
     var addressId = this.id.substring(0, this.id.indexOf("-")); 

     navigator.geolocation.getCurrentPosition(function(position) { 
     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 
      "location": new google.maps.LatLng(position.coords.latitude, position.coords.longitude) 
     }, 
     function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) 
      $("#" + addressId).val(results[0].formatted_address); 
      else 
      $("#error").append("Unable to retrieve your address<br />"); 
     }); 
     }, 


     function(positionError){ 
     $("#error").append("Error: " + positionError.message + "<br />"); 
     }, 
     { 
     enableHighAccuracy: true, 
     timeout: 10 * 1000 // 10 seconds 
     }); 
    }); 

    $("#calculate-route").submit(function(event) { 
     event.preventDefault(); 
     calculateRoute($("#from").val(), $("#to").val()); 
    }); 


    }); 
</script> 
<style type="text/css"> 
    #map { 
    width: 500px; 
    height: 400px; 
    margin-top: 10px; 
    } 
</style> 
    </head> 
    <body> 
    <h1>Calculate your route</h1> 
    <form id="calculate-route" name="calculate-route" action="#" method="get"> 
    <label for="from">From:</label> 
    <input type="text" id="from" name="from" required="required" placeholder="An address" size="30" /> 
    <a id="from-link" href="#">Get my position</a> 
    <br /> 

    <label for="to">To:</label> 
    <select id="to"> 
    <option value="51.5548885,-0.108438">Arsenal's Emirates Stadium</option> 
    <option value="51.481663,-0.1931505">Chelsea's Stamford Bridge</option> 
    </select> 

    <br /> 

    <input type="submit" /> 
    <input type="reset" /> 
</form> 
<div id="map"></div> 
<p id="error"></p>