1. 程式人生 > >openlayers 根據經緯度轉換為螢幕座標

openlayers 根據經緯度轉換為螢幕座標

如下,

centerLon ,centerLat  為獲取到的經緯度;

dx ,dy為該點相對於地圖左上角的畫素差

tmap.div.offsets[0],tmap.div.offsets[1] 為地圖左上角的螢幕座標。

PosX, PosY 為該經緯度的螢幕座標。

IE,360,firefox,google瀏覽器測試通過。

var feature = e.feature;

                var tmap = feature.layer.map;
                //通過div實現,未完成,IE和firefox都不對
                var bounds = feature.geometry.bounds;
                var centerLon = (bounds.left + bounds.right) / 2.0;
                var centerLat = (bounds.bottom + bounds.top) / 2.0;
                var resolution = tmap.getResolution();
                var extent = tmap.getExtent();
                var size = tmap.size;
                var dx =parseInt( size.w * (centerLon - extent.left) / (extent.right - extent.left));
                var dy = parseInt(size.h * (centerLat - extent.top) / (extent.bottom - extent.top));
                
                
                var PosX = tmap.div.offsets[0] + dx;
                var PosY = tmap.div.offsets[1] + dy;
                /*
                var isIE = (document.all) ? true : false;
                var PosX = isIE ? event.x : e.pageX;
                var PosY = isIE ? event.y : e.pageY;*/

                //新增一個熱區div
                var hotspotdiv = document.getElementById(_hotspotdivid);
                hotspotdiv.innerHTML = feature.attributes["Title"];
                hotspotdiv.style.left = PosX + "px";
                hotspotdiv.style.top = PosY + "px";
                hotspotdiv.style.position = "absolute";
                hotspotdiv.style.visibility = "visible";