2017-02-18 3 views
0

Итак, ребята, вы можете мне помочь? Я новичок в этой google maps api. Я хочу сделать маршрут между двумя точками. Пункт 1 - это наше текущее местоположение, а точка 2 взята из базы данных. Вот мой код. Обе точки можно показать, но мои коды не работали для маршрутаКак показать маршрут между двумя точками в Google Maps 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>

ответ

1

Рабочая Fiddle

и Documentation

Это может помочь.

function mapLocation() { 
    var directionsDisplay; 
    var directionsService = new google.maps.DirectionsService(); 
    var map; 

    function initialize() { 
     directionsDisplay = new google.maps.DirectionsRenderer(); 
     var chicago = new google.maps.LatLng(37.334818, -121.884886); 
     var mapOptions = { 
      zoom: 7, 
      center: chicago 
     }; 
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
     directionsDisplay.setMap(map); 
     google.maps.event.addDomListener(document.getElementById('routebtn'), 'click', calcRoute); 
    } 

    function calcRoute() { 
     var start = new google.maps.LatLng(37.334818, -121.884886); 
     //var end = new google.maps.LatLng(38.334818, -181.884886); 
     var end = new google.maps.LatLng(37.441883, -122.143019); 

     var bounds = new google.maps.LatLngBounds(); 
     bounds.extend(start); 
     bounds.extend(end); 
     map.fitBounds(bounds); 
     var request = { 
      origin: start, 
      destination: end, 
      travelMode: google.maps.TravelMode.DRIVING 
     }; 
     directionsService.route(request, function (response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
       directionsDisplay.setDirections(response); 
       directionsDisplay.setMap(map); 
      } else { 
       alert("Directions Request from " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed: " + status); 
      } 
     }); 
    } 

    google.maps.event.addDomListener(window, 'load', initialize); 
} 
mapLocation();