【发布时间】:2012-05-29 19:24:33
【问题描述】:
我将this jQuery plugin 用于我的Google 地图。
我有一个小表格,用于过滤地图上的标记。
标记是使用插件的$('#map_canvas').gmap('addMarker', {...}) 函数创建的,使用该函数创建标记时我可以传递的属性之一是bounds:true。它的作用是 - 当所有标记都创建并显示后,地图应该放大到地图上所有标记的边界。
问题是 - 出于某种原因,当我过滤时(按 提交 按钮),地图没有放大到新的标记。它只是停留在原来的位置。
这是为什么呢?如何使其放大到新标记的边界?
jQuery:
// when map is initialized, plot all the markers
$('#map_canvas').gmap(mapOptions).bind('init', function(){
$.getJSON('myscript.php', function(json){
var theMarkers = json;
$.each(theMarkers, function(i, val) {
$('#map_canvas').gmap('addMarker', { 'position': new google.maps.LatLng(val.Lat, val.Lng), 'bounds':true, 'icon':'myicon.png' } ).click(function(){
$('#map_canvas').gmap('openInfoWindow', { 'content': '<h1>'+val.Name+'</h1>'+'<h2 style="color: grey">'+val.Address+'</h2><p style="color: green">'+val.Telephone+'</p>' }, this);
});
});
});
});
// upon clicking submit button - clear old markers and plot newly filtered ones on the map
$('#myform').on('submit', function(e) {
$('#map_canvas').gmap('clear', 'markers');
e.preventDefault();
var myData = $('#myform').serializeArray();
$.getJSON('myscript.php', myData, function(json){
var theMarkers = json;
$.each(theMarkers, function(i, val) {
$('#map_canvas').gmap('addMarker', { 'position': new google.maps.LatLng(val.Lat, val.Lng), 'bounds':true, 'icon':'myicon.png' } ).click(function(){
$('#map_canvas').gmap('openInfoWindow', { 'content': '<h1>'+val.Name+'</h1>'+'<h2 style="color: grey">'+val.Address+'</h2><p style="color: green">'+val.Telephone+'</p>' }, this);
});
});
});
});
【问题讨论】:
标签: google-maps google-maps-api-3 jquery-ui-map