2012-07-28 78 views
0

我需要标记和地图边界之间的当前距离来决定:在tip标记上方或下方绘制tipbox。当前标记与地图边界的距离

我设定的这行代码的tipbox的位置:

Label.prototype.draw = function() { 
    var projection = this.getProjection(); 
    var position = projection.fromLatLngToDivPixel(this.get('position')); 

    // position of tipbox, depends on margin to map boaunds 
    var div = this.div_; 
    var mapWidth = parseInt($('#mapBox').css('width')); 
    var mapHeight = parseInt($('#mapBox').css('height')); 

    div.style.left = (position.x-(parseInt(div.style.width)/2)) + 'px'; 
    div.style.top = position.y + 'px';  
    div.style.display = 'inline'; 

    $('.tipBox-inner').html(''+position.x); 
}; 

但是,如果我拖动页面时,position.x或position.y返回相同的像素数。正如你在图片上看到的那样,x的位置是300px(!)。如果标记位于页面底部等,我会在标记顶部绘制tipbox。

enter image description here

回答

0

好吧,我找到了解决办法我自己。我得到的距离(PX)保证金在标记的鼠标悬停 - 事件:

google.maps.event.addListener(marker, 'mouseover', function(event) {  
    label = new Label({ 
    map: map 
    }); 

    label.bindTo('position', marker, 'position'); 

    var pixel = label.getProjection().fromLatLngToContainerPixel(event.latLng); 

    label.set('mouseX', pixel.x); 
    label.set('mouseY', pixel.y); 
}); 

现在我可以在标签与this.get(“mouseX”)

平局,函数f使用mouseX和mouseY的