2010-11-29 191 views

回答

1

就我个人而言,我不使用任何谷歌的自定义覆盖脚本等。我创建了一个自定义的php页面,其中包含iframe,我可以在其中加载自定义css文件,还可以创建自定义的DIV,这些自定义的DIV会在地图上方重新定位,然后在打开时拖动。

您可以使用“Drag,Dragstart,Dragend”事件来帮助您重新定位已创建的元素。

如果已经设置到你网页自定义标记你可以用这个功能的像素位置:

getPosition: function (marker) { 
     var map = this.map /* Your map, in my case is part of my controller object linked to this.map */; 
     var scale = Math.pow(2, map.getZoom()); 
     var nw = new google.maps.LatLng(
      map.getBounds().getNorthEast().lat(), 
      map.getBounds().getSouthWest().lng() 
     ); 
     var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw); 
     var worldCoordinate = map.getProjection().fromLatLngToPoint(marker.getPosition()); 
     var pixelOffset = new google.maps.Point(
      Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale), 
      Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale) 
     ); 
     return pixelOffset; /* Returns the top left pixel of the specified marker on the specified map */ 
    } 

然后我用它当窗口被拖动使用setPosition两种功能,它设置您的自定义元素的位置到标记的位置。 拖动事件可以这样设置: google.maps.addEventListener(map,'drag',function(){setPosition(marker,element);});

SETPOSITION功能功能简单地收集的宽度,元件的高度,并且像素偏移相关联,以使用为getPosition(标记)功能标记:

setPosition: function (marker,element) { 
    var p = this.getPosition(marker), 
     s = {width:element.offsetWidth,height:element.offsetHeight}, 
     markerOffset = {width:58,height:58}; 
    element.style.left = p.x - (s.width/2) + (markerOffset.width/2) + "px"; /* We set the element's left position to the marker's position + half of our element's width + half of the marker's width's so it is centered */ 
    element.style.top = p.y - s.height - (markerOffset.height) + 10 + "px"; /* We set the element's top position to the marker's position + our element's height + markers height so it is 10px above our marker vertically */ 
}, 

有时你必须考虑外部的一个位盒子