OpenLayers 教程
在地图开发过程中,尤其是涉及手持设备,有时会遇到GPS原始坐标数据(116°23’28.44",39°54’25.77"),为了方便使用,需要转换为经纬度(116.39123,39.9071583)。
这里介绍一下GPS坐标和经纬度坐标互转。
Openlayers GPS(度分秒)和经纬度坐标相互互转换
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://openlayers.vip/examples/css/ol.css" type="text/css">
<style>
.map {
height: 400px;
width: 100%;
float: left;
}
</style>
<script src="http://openlayers.vip/examples/resources/ol.js"></script>
<script src="./tiandituLayers.js"></script>
<title>OpenLayers example</title>
</head>
<body>
<h2>Feature transfer</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
var map = new ol.Map({
target: 'map',
layers: [
getIMG_CLayer(),
getIBO_CLayer(),
getCIA_CLayer(),
],
view: new ol.View({
projection: "EPSG:4326",
center: [116, 39],
zoom: 4,
maxZoom: 18,
minZoom: 1,
})
});
var xy = [116.391232637988, 39.907157016256974];
var originPoint = new ol.Feature({
geometry: new ol.geom.Point(xy),
name: 'My Point'
});
var layer = initVectorLayer();
function initVectorLayer() {
let source = new ol.source.Vector();
let customVectorLayer = new ol.layer.Vector({
source: source,
zIndex: 2,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 5,
lineDash: [3, 5]
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.3)',
}),
image: new ol.style.Circle({
radius: 9,
fill: new ol.style.Fill({
color: 'red',
})
})
}),
});
map.addLayer(customVectorLayer);
customVectorLayer.getSource().addFeatures([originPoint]);
var extent = customVectorLayer.getSource().getExtent();
map.getView().fit(extent, {
duration: 1,
callback: null,
});
return customVectorLayer;
}
function addFeature(geom, color) {
let temp = new ol.Feature({
geometry: new ol.geom.Point(geom),
name: 'My Point'
});
let style = new ol.style.Style({
image: new ol.style.Circle({
radius: 9,
fill: new ol.style.Fill({
color: color || 'blue',
})
})
});
temp.setStyle(style);
layer.getSource().addFeatures([temp]);
move();
return temp;
}
function convertGPSToXY(dfm) {
const arr1 = dfm.split('°');
const d = arr1[0];
const arr2 = arr1[1].split("'")
let f = arr2[0] || 0;
const m = arr2[1].replace('"', '') || 0;
f = parseFloat(f) + parseFloat(m / 60);
var du = parseFloat(f / 60) + parseFloat(d);
return du;
}
function convertXYToGPS(point) {
let xy;
if (point instanceof Array) {
xy = point;
} else {
point = point + "";
xy = point.split(',');
}
let dPoint = [];
let dPointStr = "";
for (let i = 0; i < xy.length; i++) {
const mElement = xy[i] + "";
const arr1 = mElement.split(".");
const d = arr1[0];
let tp = "0." + arr1[1]
tp = String(tp * 60);
const arr2 = tp.split(".");
const f = arr2[0];
tp = "0." + arr2[1];
tp = tp * 60;
const m = tp.toFixed(2);
const dfm = d + "°" + f + "'" + m + "\"";
dPointStr += "," + dfm;
dPoint.push(dfm);
}
dPointStr = dPointStr.replace(',', '');
return point instanceof Array ? dPoint : dPointStr;
}
var gps;
function GPSToXY() {
if (!gps) {
alert("请先点击 XY坐标转为GPS坐标!");
return;
}
if (gps instanceof Array) {
alert("XY数组:" + [convertGPSToXY(gps[0]), convertGPSToXY(gps[1])]);
} else {
alert("X或Y:" + convertGPSToXY(gps));
}
}
function XYToGPS() {
var point = originPoint.getGeometry().getCoordinates();
gps = convertXYToGPS(point);
alert("gps数组:" + gps);
}
</script>
<button id="WKTToGPS" onclick="XYToGPS()">XY坐标转为GPS坐标</button>
<button id="GPSToWKT" onclick="GPSToXY()">GPS坐标转为XY坐标</button>
</body>
</html>
在线示例
Openlayers GPS(度分秒)和经纬度坐标相互互转换:Openlayers transfer_gps
|