<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <link rel="stylesheet" href="lib/OpenLayers/ol.css" type="text/css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script src="lib/OpenLayers/ol.js"></script> <script src="olStyle/Light.js"></script> <style> html, body { width: 100%; height: 100%; margin: 0; } .map { width: 100%; height: 100%; background: #f6f6f4; } </style> </head> <body> <div id="map" class="map" data-id="11"></div> <script type="text/javascript"> $(function () { InitMap(); AddPolygon(); }) var map; var layer; //地圖初始化 function InitMap() { //初始化地圖 layer = new ol.layer.Vector({ source: new ol.source.Vector(), overlaps: false, wrapX: false }), style: function (feature, resolution) { switch (feature.get('layer')) { case 'poi': poiStyle.getText().setText(feature.get('name')); return poiStyle; case 'boundary': return boundaryStyle; case 'lawn': return lawnStyle; case 'road': roadStyle.getText().setText(feature.get('name')); return (resolution < 2) ? roadStyle : null; case 'building': return buildingStyle(feature, resolution); case 'other': otherStyle.getText().setText(feature.get('name')); return otherStyle; default: return null; } } }); map = new ol.Map({ layers: [layer], target: 'map', view: new ol.View({ center: ol.proj.fromLonLat([120.277, 36.330]), minZoom: 1, zoom: 16 }) }); } /*顯示並編輯區域**********************************************************************************/ function createLabelStyle(feature) { //返回一個樣式 return new ol.style.Style({ //把點的樣式換成ICON圖標 fill: new ol.style.Fill({ //填充顏色 color: 'rgba(37,241,239,0.2)' }), //圖形樣式,主要適用於點樣式 image: new ol.style.Circle({ //半徑大小 radius: 7, //填充 fill: new ol.style.Fill({ //填充顏色 color: '#e81818' }) }), //層 zIndex: 20 }); }; function AddPolygon() { var coordinate = ol.proj.fromLonLat([120.277, 36.330]); var newFeature = new ol.Feature({ geometry: new ol.geom.Point(coordinate) }); //設置點的樣式 newFeature.setStyle(createLabelStyle(newFeature)); //清楚座標點 //layer.getSource().clear(); //將當前要素添加到矢量數據源中 layer.getSource().addFeature(newFeature); } </script> </body> </html>