openlayers 顯示點擊位置的經緯度

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ol</title>
<link href="https://openlayers.org/en/v4.5.0/css/ol.css" rel="stylesheet">
<script src="https://openlayers.org/en/v4.5.0/build/ol.js"></script>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>

</head>

<body>
<div id="map" style="width: 100%"></div>
<script type="text/javascript">
    var map = new ol.Map({
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        target: 'map',
        view: new ol.View({
          center: ol.proj.transform(
              [104, 30], 'EPSG:4326', 'EPSG:3857'),
          zoom: 10
        })
    });

    // 監聽singleclick事件
    map.on('singleclick', function(e){
		alert(e.coordinate);
		alert(ol.proj.transform(e.coordinate, 'EPSG:3857', 'EPSG:4326'));
		
        // 通過getEventCoordinate方法獲取地理位置,再轉換爲wgs84座標,並彈出對話框顯示
		alert(map.getEventCoordinate(e.originalEvent));
        alert(ol.proj.transform(map.getEventCoordinate(e.originalEvent), 'EPSG:3857', 'EPSG:4326'));
		
        var lonlat = map.getCoordinateFromPixel(e.pixel);
		alert(lonlat);
        alert(ol.proj.transform(lonlat,"EPSG:3857", "EPSG:4326")); //由3857座標系轉爲4326
     
    })
</script>
</body>
</html>

發佈了28 篇原創文章 · 獲贊 32 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章