google map 中獲取重疊圖標(GMarker)信息的方法

最近使用google map v2 api進行開發,遇到一個問題:當在地圖上創建多個Marker時,可能會出現:在一定的縮放比例下,許多Marker重疊到一起的情況。我想實現當鼠標放到該區域的時候,顯示多個Marker的信息。


在Google,百度上搜索了半天,終於找到了解決方法:


大體的思路是:


註冊mouseover 事件,獲取到鼠標的座標(經緯度),然後遍歷地圖上所有的Marker,最核心的是判斷鼠標的座標是否包含在Marker圖標座標的範圍內。使用的方法是:


function markerIconContainsLatLng(myMarker, myLatLng){
var mapProjection=map.getCurrentMapType().getProjection(),mapZoomLevel=map.getZoom(), iconSize, iconAnchorPoint,iconAnchorPointOffset, iconBoundsPointSw,iconBoundsPointNe,
iconBoundsLatLngSw, iconBoundsLatLngNe;
iconSize=myMarker.getIcon().iconSize;
iconAnchorPoint=mapProjection.fromLatLngToPixel(myMarker.getLatLng(),
mapZoomLevel);
iconAnchorPointOffset=myMarker.getIcon().iconAnchor;
iconBoundsPointSw=new GPoint(iconAnchorPoint.x-
iconAnchorPointOffset.x, iconAnchorPoint.y-iconAnchorPointOffset.y
+iconSize.height);
iconBoundsPointNe=new GPoint(iconAnchorPoint.x-iconAnchorPointOffset.x
+iconSize.width, iconAnchorPoint.y-iconAnchorPointOffset.y);
iconBoundsLatLngSw=mapProjection.fromPixelToLatLng(iconBoundsPointSw,
mapZoomLevel);
iconBoundsLatLngNe=mapProjection.fromPixelToLatLng(iconBoundsPointNe,
mapZoomLevel);
iconBounds=new GLatLngBounds(iconBoundsLatLngSw, iconBoundsLatLngNe);
if (iconBounds.containsLatLng(myLatLng)) {
return true;

} else {
return false;
}
};


原文鏈接:http://groups.google.com/group/google-maps-api/browse_thread/thread/d84111fb1f35e085?pli=1


獲取鼠標的座標的方法 http://vikku.info/programming/google-maps-v2/get-lattitude-longitude-onmouseover-and-onmouseclick-google-map-v2.htm


希望能幫到大家!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章