2011-03-04 74 views
0

我在使用map.fitbounds時遇到了一些問題。我在地圖上通過兩點,並使用方向服務。我使用map.fitbounds到中心的兩個特定點之間的地圖,遇到了其中地圖實際上最終顯示了這個代碼特定部分的世界觀問題:Google Maps API v3 JS map.fitbounds

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    window.addEvent('domready', function() {  
     initialize(); 
    }); 

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

    var clat = 52.03574; 
    var clng = 0.58363; 
    var dlat = 51.08577; 
    var dlng = -1.31712; 

    function initialize() 
    { 
     var mOptions = { 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     var map = new google.maps.Map($("mapCanvas"), mOptions); 

     directionsDisplay = new google.maps.DirectionsRenderer({ 
      'map': map, 
      'preserveViewport': true, 
      'draggable': true//, 
      //'suppressMarkers': true 
     }); 
     directionsDisplay.setPanel($("directions_panel")); 

     google.maps.event.addListener(directionsDisplay, 'directions_changed', function() { 
      currentDirections = directionsDisplay.getDirections(); 
     }); 

     var start = 'Little Yeldham, Halstead, Essex CO9 4JZ, UK'; 
     var end = 'Winchester, Hampshire SO21, UK'; 
     var request = { 
      origin: start, 
      destination: end, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING, 
      unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL 
     }; 
     directionsService.route(request, function(response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
       directionsDisplay.setMap(map); 
       directionsDisplay.setDirections(response); 
      } 
     }); 

     var collectionPoint = new google.maps.LatLng(clat, clng); 
     var deliveryPoint = new google.maps.LatLng(dlat, dlng); 
     var bounds = new google.maps.LatLngBounds(collectionPoint, deliveryPoint); 
     map.fitBounds(bounds); 
    } 

</script> 

誰能幫助或點我在正確的方向如何簡單地顯示正確的顯示距離?

回答

1

編輯:剛發現this post有類似的問題。所以一個可能的解決方案是創建一個空邊界並開始擴展它。

我自己的代碼如下所示:

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

我一直有在某些情況下完全一樣的問題。顛倒點順序已經暫時解決了我的問題,但我仍然沒有發現真正的問題是什麼,或者它是否是GMaps API錯誤。

你有沒有運氣?