查找两个给定坐标之间的道路名称

时间:2017-09-06 15:52:20

标签: google-maps google-maps-api-3 google-maps-markers

我正在开发一个行程持续时间预测器,其中我需要两个找到两个坐标之间的道路名称。任何人都可以指导我完成它。可以通过this Google Maps Roads API完成吗?但是如何为此提供GPS点?

1 个答案:

答案 0 :(得分:-1)

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Directions service</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 450px;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
    </style>
  </head>
  <body>
    <div id="floating-panel">
<div class="goforinput" style="margin: 20px 0;">
<span><input id="currentLoction" class="BennerSearch1" style="float: left;width: 100%;text-align: center;font-size: smaller;background: #fff;color: #000;" name="s" type="text" placeholder="Enter a place, Post code or landmark... |"></span>
<span><input class="BennerSearch2" type="button" value="GO!" style="background: #39c3f3;width: 100%;float: left;font-size: 19px;border-radius: 0;margin: 14px 0px 0px 0;" id="currentLoctionB"></span>
<span style="padding-top:3.5%;font-size: 11px;float:left;width: 100%;" align="center">
<p class="round1" style="font-size: 11px!important;">Click here for directions from your current location</p>
</span>
</div>
    </div>
    <div id="map"></div>
    <?php $addressFull="anand bajar indore,452001";?>
    <script>
    //var map, infoWindow;
function initMap() {
    var latitude='22.7081960';var longitude='75.8824420';
    var address = "<?php echo $addressFull;?>";
    var directionsService = new google.maps.DirectionsService;
    var directionsDisplay = new google.maps.DirectionsRenderer;
    var infoWindow = new google.maps.InfoWindow;
    var geocoder = new google.maps.Geocoder();
   geocoder.geocode({ 'address': address},function(results, status){
      latitude = results[0].geometry.location.lat();
      longitude = results[0].geometry.location.lng();

      map = new google.maps.Map(document.getElementById('map'),{zoom: 16,
      //center: new google.maps.LatLng(latitude,longitude),
      center: {lat: latitude, lng: longitude},
      mapTypeId:'roadmap'});

      directionsDisplay.setMap(map);
      var onChangeHandler = function() {calculateAndDisplayRoute(directionsService, directionsDisplay);};
      document.getElementById('currentLoctionB').addEventListener('click', onChangeHandler);

      var icons = {parking: {icon: 'http://ccpparking.com/wp-content/uploads/2017/08/parkingMap-2.png'}};
      var features = [{ position: new google.maps.LatLng(latitude,longitude),type:'parking'}];
      features.forEach(function(feature){var marker=new google.maps.Marker({position:feature.position,title:address,icon:icons[feature.type].icon,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('Location found.');
            infoWindow.open(map);
            map.setCenter(pos);
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }

      });
    });     
}

      function calculateAndDisplayRoute(directionsService, directionsDisplay) {
        directionsService.route({
          origin: '<?php echo $addressFull;?>',
          destination: document.getElementById('currentLoction').value,
          travelMode: 'DRIVING'
        }, function(response, status) {
          if (status === 'OK') {
            directionsDisplay.setDirections(response);
          } else {
            window.alert('Directions request failed due to ' + status);
          }
        });
      }
      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
        infoWindow.open(map);
      }
 </script>   
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR API KEYcallback=initMap&sensor=false"></script>

<p id='address'></p> 
  </body>
</html>