最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to re-center a map using jquery-ui-map - Stack Overflow

programmeradmin3浏览0评论

Using 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).

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?

Using 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).

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?

Share Improve this question asked Mar 5, 2012 at 7:55 user2665694user2665694
Add a ment  | 

4 Answers 4

Reset to default 6

Use this:

$('#map_canvas').gmap('get','map').setOptions({'center':origin});

Also see this link: http://code.google./apis/maps/documentation/javascript/reference.html#MapOptions

There are two ways, either get the map and use the native setter:

var map = $("#map_canvas").gmap("get", "map");
map.setCenter(origin);

Or you use the plugin option setter

$("#map_canvas").gmap("option", "center", origin);

with panTo, you can re-center the map

jQuery('#map_canvas').gmap.panTo(new google.maps.LatLng('0.00','0.00'));

you must use 'option' before position centering...

...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.

发布评论

评论列表(0)

  1. 暂无评论