2016-02-26 51 views
1

我有一個應用程序顯示數據庫中的郵編,當點擊一個按鈕時郵政編碼在彈出窗口中作爲標記在Google地圖上進行地理編碼和顯示。我試圖在地圖上顯示兩個標記之間的行車路線。我已將經地理編碼的值保存到HTML span標記中,並試圖將這些值用作路由的起點和終點。除了顯示錯誤消息'由於NOT_FOUND引起的方向請求失敗'的標記之間顯示的路線之外,一切都有效。未找到Javascript/jQuery谷歌地圖路線

任何想法如何讓路線顯示?

$(document).ready(function() { 

     $(".openMap").click(function() { 

      var directionsService = new google.maps.DirectionsService; 
      var mapLocation = $(this).prev().prev().prev().text(); 
      var secondMarker = $(this).prev().prev().text(); 
      window.markers = []; 
      var geocoder = new google.maps.Geocoder(); 

      geocoder.geocode({ address: mapLocation }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 

        var mapOptions = { center: results[0].geometry.location, zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP }; 

        map = new google.maps.Map(document.getElementById('map'), mapOptions); 

        var directionsDisplay = new google.maps.DirectionsRenderer({map: map}); 
        directionsDisplay.setMap(map); 

        var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location, title: "Departure Location" });              
       markers.push(marker); 
       $('#route1').text(results[0].geometry.location); 
      } 

     }); 
     var geocoder2 = new google.maps.Geocoder(); 

      geocoder.geocode({ address: secondMarker }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 

        var marker2 = new google.maps.Marker({ map: map, position: results[0].geometry.location, title: "Destination Location" }); 
       markers.push(marker2); 
       $('#route2').text(results[0].geometry.location); 

      var bounds = new google.maps.LatLngBounds(); 
      for (var i = 0; i < markers.length; i++) { 
      bounds.extend(markers[i].getPosition()); 
     } 
      map.fitBounds(bounds); 
      } 
     }); 

      directionsService.route({ 
      origin: $('#route1').text(), 
      destination: $('#route2').text(), 
      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); 
      } 
     }); 
     $("#map").dialog({ title: "Lift Location ", height: 500, width: 500, modal: true }); 

       }); 
    }); 
+0

我得到你的代碼中的錯誤:'未捕獲的ReferenceError:directionsDisplay不defined' – geocodezip

+0

什麼是你的CSS/HTML看起來像?請提供證明您的問題的[最小,完整,測試和可讀示例](http://stackoverflow.com/help/mcve)。 – geocodezip

+0

https://jsfiddle.net/szw6yg6g/ – perfectDisguise

回答

1

route1和route2是空的。 API不知道如何創建從「」到「」的路由。

一旦我解決了這個問題(使用包含postcode的post1和post2),我得到一個JavaScript錯誤:Uncaught ReferenceError:directionsDisplay沒有定義。

修復顯示我的路線。

proof of concept fiddle

代碼片段:

$(document).ready(function() { 
 
    var directionsDisplay; 
 
    $(".openMap").click(function() { 
 

 
    var directionsService = new google.maps.DirectionsService; 
 
    var mapLocation = $(this).prev().prev().prev().text(); 
 
    var secondMarker = $(this).prev().prev().text(); 
 
    window.markers = []; 
 
    var geocoder = new google.maps.Geocoder(); 
 
    var mapOptions = { 
 
     zoom: 15, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }; 
 

 
    map = new google.maps.Map(document.getElementById('map'), mapOptions); 
 

 
    geocoder.geocode({ 
 
     address: mapLocation 
 
    }, function(results, status) { 
 
     if (status == google.maps.GeocoderStatus.OK) { 
 

 
     map.setCenter(results[0].geometry.location); 
 

 
     directionsDisplay = new google.maps.DirectionsRenderer({ 
 
      map: map 
 
     }); 
 
     directionsDisplay.setMap(map); 
 

 
     var marker = new google.maps.Marker({ 
 
      map: map, 
 
      position: results[0].geometry.location, 
 
      title: "Departure Location" 
 
     }); 
 
     markers.push(marker); 
 
     $('#route1').text(results[0].geometry.location); 
 
     } 
 

 
    }); 
 
    var geocoder2 = new google.maps.Geocoder(); 
 

 
    geocoder.geocode({ 
 
     address: secondMarker 
 
    }, function(results, status) { 
 
     if (status == google.maps.GeocoderStatus.OK) { 
 

 
     var marker2 = new google.maps.Marker({ 
 
      map: map, 
 
      position: results[0].geometry.location, 
 
      title: "Destination Location" 
 
     }); 
 
     markers.push(marker2); 
 
     $('#route2').text(results[0].geometry.location); 
 

 
     var bounds = new google.maps.LatLngBounds(); 
 
     for (var i = 0; i < markers.length; i++) { 
 
      bounds.extend(markers[i].getPosition()); 
 
     } 
 
     map.fitBounds(bounds); 
 
     } 
 
    }); 
 
    console.log("post1:" + $('.post1').text()); 
 
    console.log("post2:" + $('.post2').text()); 
 
    directionsService.route({ 
 
     origin: $('.post1').text(), 
 
     destination: $('.post2').text(), 
 
     travelMode: google.maps.TravelMode.DRIVING 
 
    }, function(response, status) { 
 
     if (status === google.maps.DirectionsStatus.OK) { 
 
     if (!directionsDisplay || !directionsDisplay.getMap || (directionsDisplay.getMap() == null)) { 
 
      directionsDisplay = new google.maps.DirectionsRenderer({ 
 
      map: map 
 
      }); 
 
      directionsDisplay.setMap(map); 
 
     } 
 
     directionsDisplay.setDirections(response); 
 
     } else { 
 
     window.alert('Directions request failed due to ' + status); 
 
     } 
 
    }); 
 
    $("#map").dialog({ 
 
     title: "Lift Location ", 
 
     height: 500, 
 
     width: 500, 
 
     modal: true 
 
    }); 
 
    $(".selector").dialog({ 
 
     resizeStop: function(event, ui) { 
 
     google.maps.event.trigger(map, 'resize'); 
 
     } 
 
    }); 
 
    $(".selector").dialog({ 
 
     open: function(event, ui) { 
 
     google.maps.event.trigger(map, 'resize'); 
 
     } 
 
    }); 
 

 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="https://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" /> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
 
<script src="https://maps.google.com/maps/api/js"></script> 
 
<div id="map" style="display: none;"> 
 
</div> 
 
<span class='post1'>G1 3SL</span> 
 
<span class='post2'>G1 2AF</span> 
 
<br/> 
 
<button class='openMap'>View On Map</button> 
 
<br/> 
 
<span id="route1"></span> 
 
<span id="route2"></span>

+0

謝謝你的幫助! – perfectDisguise