0

I'm struggling with Openlayer's Geolocation API, using OL v. 4.4.2. For some reason, geolocation.getPosition() returns the OL coordinates array in WGS84, even though the map's view projection is something else. Here are the relevant code bits:

const extent = [548576.0,6291456.0,1548576.0,8388608.0];
const center = [327305.0,6822495.0];
const proj = new ol.proj.Projection({ code: 'EPSG:3067', units: 'm', extent: extent });
const view = new ol.View({ center: center, projection: proj, zoom: 8 });

let geolocation = new ol.Geolocation({
  projection: view.getProjection()
});

let positionFeature = new ol.Feature();
let whereat = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: positionFeature
  })
});

geolocation.on('change', function() {
  let coordinates = geolocation.getPosition();
  positionFeature.setGeometry(
   coordinates ? new ol.geom.Point(coordinates) : null);
});

let map = new ol.Map({
  layers: [basemap1, basemap2],
  target: 'map',
  overlays: [overlay],
  controls: [new ol.control.Zoom({className: 'custom-zoom'})],
  view: view
});

$('#track').change(function() {
  geolocation.setTracking(this.checked);
  this.checked ? map.addLayer(whereat) : map.removeLayer(whereat);
});

Looking at the browser console, geolocation tracking seems to work and also changes in the position are registered, but the location does not appear on the map, and I suspect this is due to the geolocation being still obviously in WGS84, unlike the map view and other layers.

Logging the geolocation, I get for example: [23.7633003,61.5019864], so obviously in WGS84, even though EPSG:3067 is intended. Any solutions or workarounds? I've thought of setting the geolocation projection initially to WGS84 and later on using ol.proj.transform to transform it to EPSG:3067, but I find this a dirty trick, since I figure the Geolocation API is supposed to do this with the "projection" property.

0

This was actually a projection definition issue. I had a duplicate definition with and without proj4 for EPSG:3067 in OpenLayers. Removed the non-proj4 definition of EPSG:3067, and now everything works as supposed to. So, it's safest to use complete proj4 definitions for all non-default OL projections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.