Javascript 函数的作用是:返回错误的坐标
我有代码在Openlayers地图中创建和修改功能。但是当使用getCoordinates()函数从要素中提取坐标时,它会返回错误的坐标。当我尝试在谷歌地图中搜索坐标时,谷歌地图找不到位置 这是我的代码:Javascript 函数的作用是:返回错误的坐标,javascript,openlayers,Javascript,Openlayers,我有代码在Openlayers地图中创建和修改功能。但是当使用getCoordinates()函数从要素中提取坐标时,它会返回错误的坐标。当我尝试在谷歌地图中搜索坐标时,谷歌地图找不到位置 这是我的代码: var raster = new TileLayer({ source: new OSM() }); var source = new VectorSource({ wrapX: false }); var vector = new VectorLayer({
var raster = new TileLayer({
source: new OSM()
});
var source = new VectorSource({ wrapX: false });
var vector = new VectorLayer({
source: source,
renderBuffer: 600,
style: new Style({
fill: new Fill({
color: "rgba(255, 255, 255, 0.5)"
}),
stroke: new Stroke({
color: "#ffcc33",
width: 3
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: "#ffcc33"})})})});
var map = new Map({
layers: [raster, vector],
target: "map",
view: new View({
center: [-11000000, 4600000],
zoom: 1
})
});
var modify = new Modify({ source: source });
map.addInteraction(modify);
var draw, snap; // global so we can remove them later
var typeSelect = document.getElementById("type");
function addInteractions() {
draw = new Draw({
source: source,
type: typeSelect.value,
});
map.addInteraction(draw);
snap = new Snap({ source: source });
map.addInteraction(snap);
}
const getData = document.getElementById("getData");
getData.addEventListener("click", function () {
const format = new GeoJSON({ featureProjection: "EPSG:3857" });
var features = source.getFeatures();
var json = format.writeFeatures(features);
console.log(json);
});
/**
* Handle change event.
*/
typeSelect.onchange = function () {
map.removeInteraction(draw);
map.removeInteraction(snap);
addInteractions();
};
addInteractions();
以及结果的示例
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":
[-239.94578665160495,-5.404682369862513]},"properties":null},{"type":"Feature","geometry":
{"type":"Point","coordinates":[-247.41559088929245,-7.170282449846823]},"properties":null}]}
小于-180或大于+180的经度值有助于指示线串和多边形已跨日期线绘制,但对点几何体不太有用,因此可以将其坐标标准化:
source.on('addfeature', function(event){
var geometry = event.feature.getGeometry();
if (geometry.getType() === 'Point') {
geometry.setCoordinates(fromLonLat(toLonLat(geometry.getCoordinates())));
}
});