2013-04-29 67 views
2

我有一個地圖顯示帶有標記的兩個位置(並自動在這兩個位置之間居中),您可以在這裏看到它:http://goo.gl/rRDCE 我是試圖將其與Directions服務整合,就像這個演示一樣:https://developers.google.com/maps/documentation/javascript/examples/directions-simple谷歌地圖API v3:通過首先加載2個位置的地圖的駕車路線

地圖應該首先加載2個位置標記,然後如果用戶選擇一個起點,它應該顯示方向到2個位置之一。

這裏是我到目前爲止(當然不行,地圖未加載)...:P http://jsfiddle.net/multiformeingegno/fhAJA/5/

JAVASCRIPT

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

function initialize() { 
    directionsDisplay = new google.maps.DirectionsRenderer(); 
var locations = [ 
    ['Place 1', 41.897467, 12.470364, 2], 
    ['Place 2', 41.896561, 12.467792, 1] 
]; 

    var infowindow = new google.maps.InfoWindow(); 

    var bounds = new google.maps.LatLngBounds(); 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
      map: map 
     }); 

     bounds.extend(marker.position); 

     google.maps.event.addListener(marker, 'click', (function (marker, i) { 
      return function() { 
       infowindow.setContent(locations[i][0]); 
       infowindow.open(map, marker); 
      } 
     })(marker, i)); 
    } 

    map.fitBounds(bounds); 

    var listener = google.maps.event.addListener(map, "idle", function() { 
     map.setZoom(17); 
     google.maps.event.removeListener(listener); 
    }); 

    directionsDisplay.setMap(map); 
} 

function calcRoute() { 
    var start = document.getElementById('start').value; 
    var end = document.getElementById('end').value; 
    var request = { 
     origin: start, 
     destination: end, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 
    directionsService.route(request, function (response, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
      directionsDisplay.setDirections(response); 
     } 
    }); 
} 

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

HTML:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<b>Start: </b> 

<select id="start" onchange="calcRoute();"> 
    <option value="chicago, il">Chicago</option> 
    <option value="st louis, mo">St Louis</option> 
    <option value="joplin, mo">Joplin, MO</option> 
    <option value="oklahoma city, ok">Oklahoma City</option> 
    <option value="amarillo, tx">Amarillo</option> 
    <option value="gallup, nm">Gallup, NM</option> 
    <option value="flagstaff, az">Flagstaff, AZ</option> 
    <option value="winona, az">Winona</option> 
    <option value="kingman, az">Kingman</option> 
    <option value="barstow, ca">Barstow</option> 
    <option value="san bernardino, ca">San Bernardino</option> 
    <option value="los angeles, ca">Los Angeles</option> 
</select> <b>End: </b> 

<select id="end" onchange="calcRoute();"> 
    <option value="41.897467, 12.470364">Place 1</option> 
    <option value="41.896561, 12.467792">Place 2</option> 
</select> 
<div id="map" style="position:relative;width:500px;height:300px"></div> 

我在做什麼錯? :)

+0

缺少的字符串密切報價在你的位置數組的第一個元素元素?或者是一個錯字... – geocodezip 2013-04-30 00:27:23

+0

這是一個錯字。固定但不是這個問題。 :D – MultiformeIngegno 2013-04-30 00:34:52

回答

2

您缺少一個google.maps.Map對象。小提琴沒有API腳本正確包括:

var myOptions = { 
    zoom: 10, 
    center: new google.maps.LatLng(-33.9, 151.2), 
    mapTypeControl: true, 
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
    navigationControl: true, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map"), 
           myOptions); 

working fiddle(更新,因此雙向作用,至少在意大利位置)

+0

謝謝,這確實是一個進步。 :) 我的代碼授予,每個縮放級別設置2個位置始終居中。看看這裏的腳本:http://goo.gl/rRDCE – MultiformeIngegno 2013-04-30 00:41:06

+0

不幸的是方向不計算時,選擇開始/結束位置.. – MultiformeIngegno 2013-04-30 00:44:03

+0

這並不令我驚訝,你不能從駕駛方向美國到羅馬 – geocodezip 2013-04-30 00:50:37