2017-03-09 11 views
0

ノグ(ジオロケーション)現在の場所から事前に定義された目的地までの経路を検索する:私は事前に定義された目的地への方向でページを以下している

<body> 
    <h3>My Google Maps Demo</h3> 
    <div id="map"></div> 
    <script> 
     function initMap() { 
     var directionsDisplay = new google.maps.DirectionsRenderer; 
     var directionsService = new google.maps.DirectionsService; 

     var lattp = <?php echo json_encode($lattp);?>; 
     var lngtp = <?php echo json_encode($lngtp);?>; 
     var zoomtp = <?php echo json_encode($zoomtp);?>; 
     var tp = {lat: JSON.parse(lattp), lng: JSON.parse(lngtp)}; 



     var map = new google.maps.Map(document.getElementById('map'), { 
      zoom: JSON.parse(zoomtp), 
      center: tp 
     }); 
     directionsDisplay.setMap(map); 

     calculateAndDisplayRoute(directionsService, directionsDisplay); 

     } 

     if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
      var pos = { 
      lat: position.coords.latitude, 
      lng: position.coords.longitude 
      }; 

     }, function() { 
      handleLocationError(true, markerme); 
     }); 
    } else { 
     // Browser doesn't support Geolocation 
     window.alert('Geolocation is not supported'); 
    } 

    function calculateAndDisplayRoute(directionsService, directionsDisplay) { 
     var latrest = <?php echo json_encode($latrest);?>; 
     var lngrest = <?php echo json_encode($lngrest);?>; 
     var rest = {lat: JSON.parse(latrest), lng: JSON.parse(lngrest)}; 

     //var selectedMode = "WALKING"; 
     directionsService.route({ 
      origin: {lat: 51.203278, lng: 2.758969}, // current position. 
      destination: rest, // restaurant. 
      // Note that Javascript allows us to access the constant 
      // using square brackets and a string value as its 
      // "property." 
      travelMode: google.maps.TravelMode.WALKING 
     }, function(response, status) { 
      if (status == 'OK') { 
      directionsDisplay.setDirections(response); 
      } else { 
      window.alert('Directions request failed due to ' + status); 
      } 
     }); 
     } 
    </script> 
    <script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWTXOkLL3td_c8oQni-qi74ICSypn7GM8&callback=initMap"> 
    </script> 
    </body> 

することができますように: example directions

これは、次のコードで完璧に取り組んでいます現在の私の位置を原点として実装しようとしたのを見てください。 は、私はすでに原点として変数posを取得しようとしましたが、これは動作していない... でもGoogleガイドは...助けるため

のおかげで、私には明確ではありませんでした!

+0

どのようなエラーが表示されますか?それはルート状態ですか? – amenadiel

+0

原点をpos-variableに変更した場合:Uncaught ReferenceError:posは関数にパラメータとして 'pos'を渡す必要があるので、 –

+0

と定義されていません。いずれか、またはグローバルスコープで使用します。 – amenadiel

答えて

0

あなたの現在のポジションは別のファンクションの中で宣言されているので、基本的に他のすべての機能には表示されません。

navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = { 
     lat: position.coords.latitude, 
     lng: position.coords.longitude 
     }; 

    }, function() { 
     handleLocationError(true, markerme); 
    }); 

私はinitMap内の自分の位置を計算し、その後calculateAndDisplayRouteにパラメータとして、あなたの現在の位置を通過ことをお勧めしたいです。

function initMap() { 
    ... 
    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: JSON.parse(zoomtp), 
     center: tp 
    }); 
    directionsDisplay.setMap(map); 

    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = { 
      lat: position.coords.latitude, 
      lng: position.coords.longitude 
     }; 
     calculateAndDisplayRoute(directionsService, directionsDisplay, pos); 

     }, function() { 
     handleLocationError(true, markerme); 
     }); 
    } else { 
    // Browser doesn't support Geolocation 
    window.alert('Geolocation is not supported'); 
    } 


} 

function calculateAndDisplayRoute(directionsService, directionsDisplay, pos) { 
    var latrest = <?php echo json_encode($latrest);?>; 
    var lngrest = <?php echo json_encode($lngrest);?>; 
    var rest = {lat: JSON.parse(latrest), lng: JSON.parse(lngrest)}; 

    //var selectedMode = "WALKING"; 
    directionsService.route({ 
     origin: pos, // current position. 
     destination: rest, // restaurant. 
     // Note that Javascript allows us to access the constant 
     // using square brackets and a string value as its 
     // "property." 
     travelMode: google.maps.TravelMode.WALKING 
    }, function(response, status) { 
     if (status == 'OK') { 
     directionsDisplay.setDirections(response); 
     } else { 
     window.alert('Directions request failed due to ' + status); 
     } 
    }); 
} 
+0

ニース!できます!助けてくれてありがとう!私は毎日勉強しています、あなたに感謝:プロの –

関連する問題