openlayer中当前位置的经纬度

latitude and longitude of current position in openlayer

我正在尝试获取当前位置的纬度和经度值。有什么方法可以获取经纬度值吗?我试图通过获取当前位置的纬度和经度值来放置制造商。

不确定您要找的到底是什么,但这里有几种方法可以根据鼠标点击或位置获取经纬度:

鼠标点击纬度和经度:

map.on('click', function(evt){
    console.info(evt.pixel);
    console.info(map.getPixelFromCoordinate(evt.coordinate));
    console.info(ol.proj.toLonLat(evt.coordinate));
    var coords = ol.proj.toLonLat(evt.coordinate);
    var lat = coords[1];
    var lon = coords[0];
    var locTxt = "Latitude: " + lat + " Longitude: " + lon;
    // coords is a div in HTML below the map to display
    document.getElementById('coords').innerHTML = locTxt;
});

鼠标move/location纬度和经度:

map.on('pointermove', function(evt) {
    //Same code as in click event
});

不确定 "getting latitude and longitude value of current position." 在您的 post

中的含义

您可以使用:

  1. var lonlat = ol.proj.toLonLat(evt.coordinate);

  2. var lonlat = ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326');

并获取为:alert("latitude : " + lonlat[1] + ", longitude : " + lonlat[0]);

内: map.on('click', function (evt) { }