私はgoogle maps apiとDirectionsサービスを使用して2都市に関する情報を取得しています。私の要求はjsonを取得し、他のものに対してはいくつかの応答を使用していますが、座標を取得する方法はわかりません。Google Maps API Javascriptから座標を取得できません
あなたは、彼らが「閉鎖」の下の「A」と「B」と表示され、「[[スコープ]]」 from the json response in the consoleが、私は
json.routes[0].legs[0].start_location.lat["[[Scopes]]"][0].b
のようなものを呼び出すことによって、それらにアクセスすることはできませんよ見ることができるように
「未定義のプロパティ '0'を読み取ることができません」というエラーが表示されます。
私はそれはそれは[[スコープ]]内であることとは何かを持っていると思いますが、私は
EDIT何をすべきかを把握することはできません。
:私のコードをクリアすることは、このようになりますgetDirectionsに時間変数が微細検索された機能が、開始および終了緯度と経度var map;
function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: {lat: 43.16, lng: -77.61}
});
var geocoder = new google.maps.Geocoder();
directionsDisplay.setMap(map);
directionsDisplay.setOptions({ suppressMarkers: true });
var onChangeHandler = function() {
directionsService.route({
origin: document.getElementById('start').value,
destination: document.getElementById('end').value,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
getDirections(response);
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
};
document.getElementById('submit').addEventListener('click', onChangeHandler);
}
function getDirections(json) {
var time = json.routes[0].legs[0].duration.value;
time = time/3600;
startCityLat = json.routes[0].legs[0].start_location.lat["[[Scopes]]"][];
startCityLon = json.routes[0].legs[0].start_location.lat["[[Scopes]]"][0].b;
endCityLat = json.routes[0].legs[0].end_location.lat;
endCityLon = json.routes[0].legs[0].end_location.lng;
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=XXXXX&callback=initMap">
</script>
は
JSONの入手方法の簡単なサンプルコードを投稿できますか?どんなリクエストをしていますか? – chrki
@chrki私はちょうどhttps://maps.googleapis.com/maps/api/js?key=XXXXXXX&callback=initMap – joshshulten