2013-05-26 41 views
0

我的地圖在頁面加載時有明確的方向。我有一個表單可以改變提交的方向。我似乎無法使用我的代碼進行這項工作。任何想法或幫助將不勝感激。谷歌地圖指示表格提交不起作用

這是我的api代碼。

<script type="text/javascript"> 
// 
    var dService = new google.maps.DirectionsService(); 
    var departurePoint = new google.maps.LatLng(,); 
    var destinationPoint = new google.maps.LatLng(,); 
function initialize() { 
    dDisplay = new google.maps.DirectionsRenderer(); 
    var map = new google.maps.Map(document.getElementById("map"), { 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    mapTypeControl: true 
    }); 
    dDisplay.setPanel(document.getElementById("directionsHolder")); 
     dDisplay.setMap(map); 
     dService.route({ 
     origin: departurePoint, 
     destination: destinationPoint, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
     }, function(result, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
      dDisplay.setDirections(result); 
     } 
     }); 
function setDirections(destinationPoint, dirLang) { 
      dDisplay.setPanel(document.getElementById("directionsHolder")); 
      dDisplay.setMap(map); 
    dService.route({ 
    origin: departurePoint, 
    destination: destinationPoint, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }, function(result, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     dDisplay.setDirections(result); 
    } 
    }); 
} 
} 
google.maps.event.addDomListener(window, 'load', initialize); 

這裏是我的表格:

 <form id="getDirections" action="#" onsubmit="setDirections(this.destinationPoint.value, this.dirLang.value); return false"> 
    <fieldset> 
     <label for="destinationPoint">Directions: </label> 
     <select id="destinationPoint" name="destinationPoint"> 
      <option value="loc1">Location 1</option> 
      <option value="loc2">Location 2</option> 
      <option value="loc3">Location 3</option> 
     </select> 

     <label for="dirLang"></label> 
     <select id="dirLang" name="dirLang"> 
      <option value="en" selected="selected" >English</option> 
      <option value="fr">French</option> 
      <option value="de">German</option> 
      <option value="es">Spanish</option> 
      <option value="ja">Japanese</option> 
      <option value="nl">Dutch</option> 
     </select> 
     <input name="submit" type="submit" class="submit" value="View Directions" /> 
    </fieldset> 
</form> 

回答

0

你有範圍的問題:

  1. 的setDirections功能是本地的初始化函數
  2. 地圖變量是本地的初始化功能

    <script type="text/javascript"> 
    // 
    var map = null; 
    var dService = new google.maps.DirectionsService(); 
    var departurePoint = "Newark, NJ"; 
    var destinationPoint = "New York, NY"; 
    function initialize() { 
        dDisplay = new google.maps.DirectionsRenderer(); 
        map = new google.maps.Map(document.getElementById("map"), { 
         mapTypeId: google.maps.MapTypeId.ROADMAP, 
         mapTypeControl: true 
        }); 
    dDisplay.setPanel(document.getElementById("directionsHolder")); 
    dDisplay.setMap(map); 
    dService.route({ 
        origin: departurePoint, 
        destination: destinationPoint, 
        travelMode: google.maps.DirectionsTravelMode.DRIVING 
        }, function(result, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         dDisplay.setDirections(result); 
        } 
        }); 
    } 
    function setDirections(destinationPoint, dirLang) { 
         dDisplay.setPanel(document.getElementById("directionsHolder")); 
         dDisplay.setMap(map); 
    dService.route({ 
        origin: departurePoint, 
        destination: destinationPoint, 
        travelMode: google.maps.DirectionsTravelMode.DRIVING 
        }, function(result, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         dDisplay.setDirections(result); 
        } 
        }); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
    

working example

+0

工作就像一個魅力!謝謝@geocodezip即時通訊相當新的谷歌地圖API,我很困惑。 – user1532487