2012-07-28 83 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的