使用 GoogleMaps 中的 Long 和 Lat 时无法在 OpenLayers 中设置坐标

Trouble to set coordinates in OpenLayers when using Long and Lat from GoogleMaps

这是德黑兰在 Google 地图上的坐标。 https://www.google.com/maps/place/Tehran/@35.6964895,51.0696315,10z/data=!3m1!4b1!4m2!3m1!1s0x3f8e00491ff3dcd9:0xf0b3697c567024bc

35.6961° N, 51.4231° E

我试图在 OpenLayers 中找到这个坐标,但我没有找到,这是我的代码:

map = new ol.Map({
    target: 'sample-map',
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        })
    ],
    view: new ol.View({         
        center:  [35.6961, 51.4231],
        zoom: 4
    })
});

我也在尝试获取以度为单位的当前坐标,但我不知道如何获取。

我不完全确定问题出在哪里,但我可以这样说:您需要将 center 上的参数交换为 [51.4231, 35.6961]

根据 Openlayers documentationcenter 的格式为 [x-axis, y-axis] 或者您的情况 [East, North].

在您的特定情况下,您的源投影不是 lat/long,因此您必须进行转换。以下代码应该适合您:

地图=新ol.Map({ 目标:'sample-map', 层数:[ 新 ol.layer.Tile({ 资料来源:新 ol.source.OSM() })], 视图:新 ol.View({<br> 中心:ol.proj.transform([51.4231, 35.6961], 'EPSG:4326', new ol.source.OSM().getProjection()), 缩放:10 }) });