【发布时间】:2011-08-25 06:05:36
【问题描述】:
到目前为止,我采取的方法是:
function addMarker( query ) {
var geocoder = new google.maps.Geocoder();
var afterGeocode = $.Deferred();
// Geocode 'query' which is the address of a location.
geocoder.geocode(
{ address: query },
function( results, status ){
if( status === 'OK' ){
afterGeocode.resolve( results ); // Activate deferred.
}
}
);
afterGeocode.then( function( results ){
var mOptions = {
position: results[0].geometry.location,
map: map
}
// Create and drop in marker.
var marker = new google.maps.Marker( mOptions );
marker.setAnimation( google.maps.Animation.DROP );
var current_bounds = map.getBounds(); // Get current bounds of map
// use the extend() function of the latlngbounds object
// to incorporate the location of the marker
var new_bounds = current_bounds.extend( results[0].geometry.location );
map.fitBounds( new_bounds ); // fit the map to those bounds
});
}
我遇到的问题是地图莫名其妙地缩小了一些量,无论新标记是否适合当前视口。
我做错了什么?
附录
我添加了日志和一个额外的变量来捕获地图边界之后进行了转换 (new_new_bounds)
current_bounds = // Map bounds before anything is done.
{-112.39575760000002, 33.60691883366427},
{-112.39295444655761, 33.639099}
new_bounds = // From after the extend
{-112.39295444655761, 33.60691883366427},
{-112.39575760000002, 33.639099}
new_new_bounds = // From after the fitbounds
{-112.33942438265382, 33.588697452015374},
{-112.44928766390382, 33.657309727063996}
【问题讨论】:
-
new_bounds 说它的边界在扩展之后是什么?
-
看起来从 (x1,y1),(x2,y2) -> (x2,y1),(x1,y2) 的边界变化引起了问题。我不是 100%,但这就是我要开始的地方。
-
首先,通过将 current_bounds 传递给 fitBounds 并查看视图是否移动,确保这不是错误。
标签: javascript google-maps google-maps-api-3 google-maps-markers