2

我正在使用jQuery Mobile的移動應用程序。我也是你的插件「jquery-ui-map」來做我的地圖(這幫助了很多顯示問題,只用GMQ和jQM)。jquery-ui-map適合界限與折線?

我可以在地圖上添加折線,它工作得很好。但是當我嘗試使用fitBounds方法時,它不起作用。事實上,它縮小了很多。但不在我的範圍內。

這裏是我的代碼:

// Added some data, so you can understand the structure 
// of variable "plan" 
plan[0].lat_a = 45.4681; plan[0].lng_a = -73.7414; 
plan[0].lat_b = 45.6797; plan[0].lng_b = -74.0386; 
plan[1].lat_a = 45.6797; plan[1].lng_a = -74.0386; 
plan[1].lat_b = 48.7753; plan[1].lng_b = -64.4786; 

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

// Starting point 
var LatLng = new google.maps.LatLng(plan[0].lat_a, plan[0].lng_a); 
bounds.extend(LatLng); 

// Building the polyline coords and bounds 
for (var x=0; x<plan.length; x++) { 
    polylinesCoords.push(new google.maps.LatLng(plan[x].lat_a, plan[x].lng_a), new google.maps.LatLng(plan[x].lat_b, plan[x].lng_b)); 
    LatLng = new google.maps.LatLng(plan[x].lat_b, plan[x].lng_b); 
    bounds.extend(LatLng); 
} 

// Drawing polyline on map 
$('#map_canvas').gmap('addShape', 'Polyline', { 
    'path': polylinesCoords, 
    'strokeColor': '#c00', 
    'strokeThickness': 5 
}); 

// «Focus» map on polyline with bounds 
var map = $("#map_canvas").gmap("get", "map"); 
map.fitBounds(bounds); 

一切都在這個片段看起來工作正常,除了最後一行。

任何想法?

+0

我已經看過了香草fitBounds,並發現它迫使你的「邊界」變量和地圖邊緣之間至少有45像素的填充工作。我不確定這個填充是用於移動的,但它可能是縮小的原因。 – 2012-04-05 15:03:56

+0

有趣的是,我會嘗試設置一個更大的地圖。我正在嘗試任何事情。 但我的視口有200px的高度(和更大的方式)。即使我刪除了45像素填充,我仍然剩下110像素。 當我完全縮小時,我看到的最多可能是10像素寬度和高度。大部分是世界地圖上的一個小點。 – 2012-04-05 18:20:50

+0

即使有一張450像素x 450像素的地圖沒有奏效。 :( – 2012-04-09 12:30:05

回答

0

您可能錯過了將地圖居中放置到您的任何針腳。我得到了你的代碼這行代碼

$('#map_canvas').gmap('option', 'center', (new google.maps.LatLng(plan[0].lat_a,plan[0].lng_a)));