9

我采取的方法(n)的标记位置自动适应窗格迄今为止一直:设置一个谷歌地图视口的各个位置

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} 
+0

什么是new_bounds说它的边界在扩展之后? – 2011-05-13 21:09:49

+0

更新了诊断程序。 – dclowd9901 2011-05-13 21:26:33

+0

它看起来像从(x1,y1),(x2,y2) - >(x2,y1),(x1,y2)的边界变化会引发问题。我不是100%,但那是我开始的地方。 – 2011-05-13 23:43:45

回答

8

好的,经过多番争论后,事实证明问题是地图边界与fitBounds()之后的地图边界不一样。会发生什么(我认为),是Google 需要您在fitBounds()方法中给出的边界,然后填充它们。每次将当前边界发送到fitBounds()时,您不会拟合边界(x,y),您将拟合边界(x + m,y + m),其中m =任意边界。

这就是说,最好的办法是这样的:

var current_bounds = map.getBounds(); 
var marker_pos = marker.getPosition(); 

if(!current_bounds.contains(marker_pos)){ 

    var new_bounds = current_bounds.extend(marker_pos); 
    map.fitBounds(new_bounds); 
} 

因此,地图只能以边界如果标记放置在当前地图范围之外的瀑布。希望这可以帮助任何碰到这个问题的人。

0

一种可能的解释是,你随意摆放你的新标记进入z曲线的间隙。 Z曲线递归将地图细分为4个较小的贴图,但这也是瓷砖之间存在间隙的原因。更好的方法是对地图应用使用希尔伯特曲线或摩尔曲线。有一个覆盖这个问题的专利搜索算法,我认为它被称为quadtrees中的多维范围查询。你想寻找尼克的希尔伯特四角树空间索引博客。

+0

我的回答很难过。我只是使用v3 API提供的标准库。查询只是地址或地址的一部分。 – dclowd9901 2011-05-13 22:44:19

+0

dclowd,我修复了我的答案! – Bytemain 2011-05-13 23:17:48