2016-05-28 1 views
-1

私はお詫びしますが、私はJavaScriptの専門家ではありません。Googleマップを使用して経路を追跡するために、変数でLatitudeとLongitudeを取得する必要があります。あなたが見ることができるように、私は正常に動作終了のCOORDSを置くためにPHPを使用して、JavaScript:varにGPSコードを入れよう

 var start = "12.3456789,12.3456789"; 

を書く場合

function calcRoute() { 
    var start = "position.coords.latitude,position.coords.longitude"; 
    var end = "<?php echo $la; ?>,<?php echo $lo; ?>"; 
    var request = { 
     origin:start, 
     destination:end, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 

このコードは動作します。私はエンド varにposition.coords.latitudeを使用するさまざまな方法を試みたが、なぜ "position.coords.latitude、position.coords.longitude"が "12.3456789,12.3456789"に変更されないのか分からない。

注:アラートを使用すると権限値が表示されるため、position.coords.latitudeとposition.coords.longitudeは正しく入力されているはずです。助けのための

おかげで...

+0

を貼り付けることができますか? –

+0

var end = "12.3456789,12.3456789"; – Charlie

+0

'position.coords'とは何ですか? JS変数?はいの場合は、定義方法とその割り当て方法を示すことができますか? –

答えて

0

あなたの問題は、JavaScriptでstringに変数を連結してあります。 position.coords.latitudeまたはが数字の場合(または文字列の場合もあります)、"12.3456789,12.3456789"のような文字列を取得するには、+演算子を使用して変数を接続する必要があります。

だからあなたが持っている必要があります:あなたのコードで

var start = position.coords.latitude + "," + position.coords.longitude; 

を。

EDIT

作業例:あなただけ正確エンドVARで出力する内容

function geocode(){ 
 
    if (navigator.geolocation) { 
 
     navigator.geolocation.getCurrentPosition(calcRoute,function(error){ 
 
     \t alert("Geolocation does not work because:" + error.message); 
 
     }); 
 
    } else { 
 
     alert("Geolocation is not supported by this browser."); 
 
    } 
 
} 
 

 
function calcRoute(position) { 
 
    console.log(position); 
 
    var mapOptions = { 
 
     zoom: 10, 
 
     center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude), 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }; 
 

 
    var map = new google.maps.Map(document.getElementById('map'), mapOptions); 
 
           
 
    var geocoder = new google.maps.Geocoder(); 
 
    var start = position.coords.latitude + "," + position.coords.longitude; 
 
    var end = "49.200147, 16.607560"; 
 
    var request = { 
 
     origin:start, 
 
     destination:end, 
 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
 
    }; 
 
    var directionsService = new google.maps.DirectionsService(); 
 
    directionsService.route(request, function(response, status) { 
 
     if (status == google.maps.DirectionsStatus.OK) { 
 
     var directionsDisplay = new google.maps.DirectionsRenderer(); 
 
     directionsDisplay.setMap(map); 
 
     directionsDisplay.setDirections(response); 
 
     }else{ 
 
     alert("Direction were not found:" + status); 
 
     } 
 
    }); 
 
}
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry&dummy=.js"></script> 
 
<body onload="geocode()"> 
 
    <div id="map" style="height:400px; width:500px;"></div> 
 
</body>

+0

はい、これは方法ですが、私は正しいsintaxを見つけることなく多くの方法を試しました。 – Charlie

+0

私は自分の答えを更新しました。あなたのサーバーでこのコードを試してみてください。 'var end =" 12.3456、12.3456 ";"をいくつかの意味のある値に置き換えてください。コードにエラーコールバックもあります。何か問題が生じた場合は、その理由を知ることができます。 –

+0

私は私のテストのURLを入れます:http://localhost/www.parcheggi.info/metropark/gmaps2.php?id = 8あなたが見ることができるように(HTMLソース)、スクリプトは "12.34567" ... – Charlie

関連する問題