javascript 如何使用 jquery-ui-map 重新居中地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9563314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to re-center a map using jquery-ui-map
提问by Andreas Jung
Using jquery-ui-map.
使用 jquery-ui-map。
I am loading data through JSON with some marker and the latitude/longitude of the new center of the map (location finder application).
我正在通过带有一些标记和地图新中心(位置查找器应用程序)的纬度/经度的 JSON 加载数据。
var origin = new google.maps.LatLng(data.origin.latitude, data.origin.longitude)
// adding the marker for the new origin works
$('#map_canvas').gmap('addMarker', {'position': origin});
// does not work
$('#map_canvas').gmap('center', '49, 7');
// does not work
$('#map_canvas').gmap('center', origin);
// does not work
$('#map_canvas').gmap('center', {'position': origin});
All of the three center calls fail:
所有三个中心呼叫都失败了:
Uncaught TypeError: Cannot call method 'apply' of undefined
$.a.$.fn.(anonymous function)jquery.ui.map.js:46
b.extend.eachjquery.js:35
b.fn.b.eachjquery.js:28
$.a.$.fn.(anonymous function)jquery.ui.map.js:40
(anonymous function)@@geosearch:477
c.extend.handleSuccessjquery.js:144
c.extend.ajax.w.onreadystatechange
What is the canonical way here?
这里的规范方式是什么?
回答by Engineer
Use this:
用这个:
$('#map_canvas').gmap('get','map').setOptions({'center':origin});
Also see this link: http://code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions
另请参阅此链接:http: //code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions
回答by johansalllarsson
There are two ways, either get the map and use the native setter:
有两种方法,要么获取地图,要么使用原生 setter:
var map = $("#map_canvas").gmap("get", "map");
map.setCenter(origin);
Or you use the plugin option setter
或者你使用插件选项设置器
$("#map_canvas").gmap("option", "center", origin);
回答by Roberto
with panTo, you can re-center the map
使用 panTo,您可以将地图重新居中
jQuery('#map_canvas').gmap.panTo(new google.maps.LatLng('0.00','0.00'));
回答by ilConfa
you must use 'option'
before position centering...
您必须'option'
在位置居中之前使用...
...like example below (I used button click event and some other option):
...就像下面的例子(我使用了按钮点击事件和其他一些选项):
$("#button").click( function(){
var latLng = new google.maps.LatLng(data.origin.latitude, data.origin.longitude);
$('#map_canvas').gmap('option', 'center', latLng);
$('#map_canvas').gmap('option', 'zoom', 7);
$('#map_canvas').gmap('addMarker', {position: latLng,bounds: false});
});
hope help you.
希望能帮到你。