<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenLayers with Static World Map</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@7.2.2/ol.css">
<style>
#map {
width: 30%;
height: 30vh;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@7.2.2/dist/ol.js"></script>
<script>
let data = [
{
"longitude": "0.0",
"latitude": "0.0"
},
{
"longitude": "1.0",
"latitude": "0.5"
},
{
"longitude": "2.0",
"latitude": "1.0"
},
{
"longitude": "3.0",
"latitude": "1.5"
},
];
const imageExtent = [-180, -90, 180, 90];
const map = new ol.Map({
target: "map",
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
url: "bg2.jpg",
imageExtent: imageExtent,
projection: "EPSG:4326",
}),
}),
],
view: new ol.View({
projection: "EPSG:4326",
center: [0, 0],
zoom: 1,
minZoom: 1,
maxZoom: 18,
}),
});
const points = [];
data.forEach((item, index) => {
if (
item.longitude >= -180 &&
item.longitude <= 180 &&
item.latitude >= -90 &&
item.latitude <= 90
) {
points.push([parseFloat(item.longitude), parseFloat(item.latitude)]);
} else {
console.log("Invalid point:", item.longitude, item.latitude);
}
});
const pointStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 1,
fill: new ol.style.Fill({ color: "red" }),
stroke: new ol.style.Stroke({ color: "red", width: 2 }),
}),
text: new ol.style.Text({
text: "",
font: "12px Arial",
fill: new ol.style.Fill({ color: "black" }),
offsetY: -10,
}),
});
const lineStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: "red",
width: 2,
}),
});
const lineLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: lineStyle,
});
const lineString = new ol.geom.LineString(points);
const lineFeature = new ol.Feature({
geometry: lineString,
});
lineLayer.getSource().addFeature(lineFeature);
map.addLayer(lineLayer);
vue3 写法如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenLayers with Static World Map</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@7.2.2/ol.css">
<style>
#map {
width: 30%;
height: 30vh;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@7.2.2/dist/ol.js"></script>
<script>
let data = [
{
"longitude": "0.0",
"latitude": "0.0"
},
{
"longitude": "1.0",
"latitude": "0.5"
},
{
"longitude": "2.0",
"latitude": "1.0"
},
{
"longitude": "3.0",
"latitude": "1.5"
},
];
const imageExtent = [-180, -90, 180, 90];
const map = new ol.Map({
target: "map",
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
url: "bg2.jpg",
imageExtent: imageExtent,
projection: "EPSG:4326",
}),
}),
],
view: new ol.View({
projection: "EPSG:4326",
center: [0, 0],
zoom: 1,
minZoom: 1,
maxZoom: 18,
}),
});
const points = [];
data.forEach((item, index) => {
if (
item.longitude >= -180 &&
item.longitude <= 180 &&
item.latitude >= -90 &&
item.latitude <= 90
) {
points.push([parseFloat(item.longitude), parseFloat(item.latitude)]);
} else {
console.log("Invalid point:", item.longitude, item.latitude);
}
});
const pointStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 1,
fill: new ol.style.Fill({ color: "red" }),
stroke: new ol.style.Stroke({ color: "red", width: 2 }),
}),
text: new ol.style.Text({
text: "",
font: "12px Arial",
fill: new ol.style.Fill({ color: "black" }),
offsetY: -10,
}),
});
const lineStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: "red",
width: 2,
}),
});
const lineLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: lineStyle,
});
const lineString = new ol.geom.LineString(points);
const lineFeature = new ol.Feature({
geometry: lineString,
});
lineLayer.getSource().addFeature(lineFeature);
map.addLayer(lineLayer);