0
여러분, 저를 도와 줄 수 있습니까? Im이 Google에있는 초보자 API 것들을 매핑합니다. 2 포인트 사이의 경로를 만들고 싶습니다. 포인트 1은 현재 위치이고 포인트 2는 데이터베이스에서 가져온 것입니다. 여기 내 코드가있다. 두 지점이 표시 될 수 있지만, 내 코드는Google지도 API에서 두 지점 사이의 경로를 표시하는 방법은 무엇입니까? 포인트 A는 현재 위치이고 포인트 B는 데이터베이스에서 가져옵니다
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
function initMap() {
\t directionsDisplay = new google.maps.DirectionsRenderer();
var myLatlng = new google.maps.LatLng(<?php echo $lat ?>,<?php echo $long ?>);
var mapOptions = {
zoom: 13,
center: myLatlng
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
var marker = new google.maps.Marker({
position: myLatlng,
title:"Hello World!"
});
// To add the marker to the map, call setMap();
marker.setMap(map);
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('You are here');
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
// get route from A to B
calculateAndDisplayRoute(directionsService, directionsDisplay, infoWindow, marker);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay, infoWindow, marker) {
directionsService.route({
origin: infoWindow,
destination: marker,
avoidTolls: true,
avoidHighways: false,
travelMode: google.maps.TravelMode.DRIVING
}, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
initMap();
</script>