简体   繁体   English

Openlayers3获取更改事件的地图坐标

[英]Openlayers3 get map coordinates on change event

I use a map event to check the coordinates of the map when a user interacts with it. 当用户与地图进行交互时,我使用地图事件来检查地图的坐标。 I then pass this new map coordinates to cookies so that next time the user accesses the map it remember the coordinates. 然后,我将此新地图坐标传递给Cookie,以便下次用户访问地图时记住该坐标。

I'm having issues retrieving the correct latitude and longitude projection. 我在检索正确的纬度和经度投影时遇到问题。 I'm using mapbox for the source. 我正在使用mapbox作为源。

This is the initial coordinates: 这是初始坐标:

[55.378051, -3.43597299999999] = UK

Then I use a map change event to grab the coordinates for the center of the map. 然后,我使用地图更改事件来获取地图中心的坐标。

map.getView().on('change:center', function) {

   var coord = ol.proj.transform(map.getView().getCenter(), 'EPSG:3857', 'EPSG:4326');   
   var lon = coord[1];
   var lat = coord[0];

   Cookies.set(_myLonCoord, lon);
   Cookies.set(_myLatCoord, lat);

});

If I move the map to Germany this is the coordinates that I get: [8.686417,50.057008999999994] which seem totally correct. 如果我将地图移到德国,这就是我得到的坐标:[8.686417,50.057008999999994]这似乎是完全正确的。

Then I load the page again, and the map view is off the coast in South Africa. 然后,我再次加载页面,地图视图在南非的海岸外。 Some projection issues are happening here. 这里有一些投影问题。

I keep getting errors on my console and I have tried changing the projection transformation many times. 我一直在控制台上出现错误,并且尝试过多次更改投影变换。

ol.js:249 Uncaught TypeError: Failed to execute 'putImageData' on 'CanvasRenderingContext2D': The provided double value is non-finite.

Make sure you're transforming this cookie coordinates back to EPSG:3857 , also make sure you're dealing with numbers instead of string , see parseFloat() : 确保将这个cookie坐标转换回EPSG:3857 ,并确保您正在处理numbers而不是string ,请参阅parseFloat()

// a helper function
function to3857(coord) {
  return ol.proj.transform(
    [parseFloat(coord[0]), parseFloat(coord[1])],
    'EPSG:4326', 'EPSG:3857'
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM