<!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>
openlayers 显示点击位置的经纬度
最新推荐文章于 2025-01-12 17:01:26 发布