jquery-ui-map用折线拟合边界?

时间:2012-04-05 14:42:23

标签: google-maps jquery-plugins jquery-mobile google-polyline jquery-ui-map

我正在使用jQuery Mobile开发移动应用程序。我也是你的插图“jquery-ui-map”来做我的地图(这有助于很多只使用GMAP和jQM的显示问题)。

我可以在地图上添加折线,效果很好。但是当我尝试使用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);

此片段中的所有内容似乎都运行正常,但最后一行除外。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可能错过了将地图置于任何针脚的中心位置。我让你的代码使用这行代码

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