gpt4 book ai didi

javascript - 当我尝试更改 map View 时 OpenLayers 出错

转载 作者:行者123 更新时间:2023-12-01 00:58:51 28 4
gpt4 key购买 nike

我正在尝试开始使用 OpenLayers 和 Nominatim 进行地理编码,但是当我尝试更改 map 的 View 时(例如,因为用户输入了一个地址,并且我想将 map 置于其中心),我得到了错误:

ol-debug.js:6171 Uncaught TypeError: Cannot read property 'getCode' of null
at Object.ol.proj.getTransformFromProjections (ol-debug.js:6171)
at Object.ol.proj.getTransform (ol-debug.js:6155)
at Object.ol.proj.transform (ol-debug.js:6251)
at Object.ol.proj.fromLonLat (ol-debug.js:6063)
at Object.<anonymous> ((index):74)
at c (jquery-3.4.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.4.1.min.js:2)
at l (jquery-3.4.1.min.js:2)
at XMLHttpRequest.<anonymous> (jquery-3.4.1.min.js:2)

这是我的代码(大部分只是我在网上找到的示例代码的混搭):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>OpenStreetMap &amp; OpenLayers - Marker Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v4.6.5/build/ol-debug.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>

<script>
/* OSM & OL example code provided by https://mediarealm.com.au/ */
var map;
var mapLat = -33.829357;
var mapLng = 150.961761;
var mapDefaultZoom = 10;
var view = new ol.View({
center: ol.proj.fromLonLat([mapLng, mapLat]),
zoom: mapDefaultZoom
})
function initialize_map() {
map = new ol.Map({
target: "map",
layers: [
new ol.layer.Tile({
source: new ol.source.OSM({
url: "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png"
})
})
],
view: view
});
}
function add_map_point(lat, lng) {
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([parseFloat(lng), parseFloat(lat)], 'EPSG:4326', 'EPSG:3857')),
})]
}),
style: new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 0.5],
anchorXUnits: "fraction",
anchorYUnits: "fraction",
src: "https://upload.wikimedia.org/wikipedia/commons/e/ec/RedDot.svg"
})
})
});
map.addLayer(vectorLayer);
}

function geocode() {
var address = $("#address").val();

console.log(address);

var data = {
"format": "json",
"addressdetails": 1,
"q": address,
"limit": 1
};
$.ajax({
method: "GET",
url: "https://nominatim.openstreetmap.org",
data: data
})
.done(function (msg) {
console.log(msg);
add_map_point(msg[0].lat, msg[0].lon);
view.center = ol.proj.fromLonLat(msg[0].lon, msg[0].lat);
});
}
</script>
</head>
<body onload="initialize_map();">
<input type="text" id="address" />
<button type="submit" id="submit" onclick="geocode();">Submit</button>
<div id="map" style="width: 100vw; height: 100vh;"></div>
</body>
</html>

因此,我在文本框中输入地址或城市或其他内容,然后单击提交按钮 - map 上搜索位置处会出现一个红点,但 map 不会以该位置为中心;相反,我收到了我之前描述的错误。可能出了什么问题?

编辑:将 view.center 行替换为:

view.animate({
center: ol.proj.fromLonLat([msg[0].lon, msg[0].lat]),
duration: 2000});

这使它做了一些事情 - 但它的中心位于海洋中央!

最佳答案

view.center 应该是 view.setCenter()

view.setCenter([ol.proj.fromLonLat(msg[0].lon, msg[0].lat]);

如果经/纬度值是字符串,则需要将其转换为 float

view.setCenter([ol.proj.fromLonLat(parseFloat(msg[0].lon), parseFloat(msg[0].lat)]);

你也可以尝试

view.animate({
center: ol.proj.fromLonLat([parseFloat(msg[0].lon), parseFloat(msg[0].lat)]),
duration: 2000});

关于javascript - 当我尝试更改 map View 时 OpenLayers 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56298609/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com