2013-12-12 36 views

回答

0

您可以在初始化地圖時使用任何虛擬位置。如果您知道兩點的座標,則可以使用LatLngBounds確保兩個點都顯示在地圖上。

var bounds = new google.maps.LatLngBounds(); 
bounds.extend(latlng1); 
bounds.extend(latlng2); 
map.fitBounds(bounds); 

這個代碼採取兩個點,並確保谷歌地圖檢視區既包括這些點,同時顯示。因此你不需要計算中心。

+0

謝謝久勝你的迴應。這真的很有用。 – Deepjyot

0

如果您使用的是Gooogle Maps Javascript API v3directions service,那麼默認情況下,該地圖集中顯示整個路線。

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

    function initialize() { 
     directionsDisplay = new google.maps.DirectionsRenderer(); 
     var mapOptions = {}; 
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
     directionsDisplay.setMap(map); 
     calcRoute(); 
    } 

    function calcRoute() { 
     var start = 'Chicago, IL'; 
     var end = 'St Louis, MO'; 
     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); 
     } 
     }); 
    } 

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

working example

+0

謝謝geocodezip。你的迴應非常有用。 – Deepjyot

相關問題