1. 程式人生 > >OpenLayer4.6.5繪製和編輯要素及獲取定位點

OpenLayer4.6.5繪製和編輯要素及獲取定位點

OpenLayer4.6.5繪製和編輯要素及獲取定位點

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>繪製修改要素</title>
        <script src="../jslib/jquery-1.7.2.min.js"></script
>
<link rel="stylesheet" href="../css/ol.css" type="text/css"> <script src="../jslib/ol.js"></script> </head> <body> <div id="map" class="map" style="height: 80%;"> </div> <label>幾何型別 &nbsp;</label>
<select id="type"> <option value="Point">Point</option> <option value="LineString">LineString</option> <option value="Polygon">Polygon</option> <option value="Circle">Circle</option> </select
>
<script> var raster=new ol.layer.Tile({ source:new ol.source.OSM() }); var source=new ol.source.Vector(); var vector=new ol.layer.Vector({ source:source, style:new ol.style.Style({ fill:new ol.style.Fill({ color: 'rgba(255, 255, 255, 0.2)' }), stroke:new ol.style.Stroke({ color:'#000', width:5 }), image:new ol.style.Circle({ radius:7, fill:new ol.style.Fill({ color:"#F00" }) }) }) }); var map=new ol.Map({ layers:[raster,vector], target:"map", view:new ol.View({ center:[0,0], zoom:4 }) }) var modify=new ol.interaction.Modify({source:source}); map.addInteraction(modify); var draw=null; var snap=null; function addInteractions(){ draw=new ol.interaction.Draw({ source:source, type:$("#type").val() }); map.addInteraction(draw); snap=new ol.interaction.Snap({ source:source }); map.addInteraction(snap); } //選擇樣式 $("#type").change(function(){ map.removeInteraction(draw); map.removeInteraction(snap); addInteractions(); }); addInteractions();//預設為點樣式 map.on("click",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>