I have tried to make a simple map with OL 3's Geolocation module. I succeeded with a resource wasting method. There must be a simpler way to extract variable information from an OpenLayers 3 object. My code is the following:

var view = new ol.View({
    center: [0, 0],
    zoom: 16

var map = new ol.Map({
    target: 'map',
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
    view: view

var geolocation = new ol.Geolocation({
    projection: view.getProjection()


var geolocation2 = new ol.Geolocation({
    projection: view.getProjection()


var featureSource = new ol.source.Vector({

var feature = new ol.Feature();

feature.bindTo('geometry', geolocation, 'position')
    .transform(function() {}, function(coordinates) {
      return coordinates ? new ol.geom.Point(coordinates) : null;


var featureLayer = new ol.layer.Vector({
    source: featureSource,
    name: 'Your position'

view.bindTo('center', geolocation2, 'position');


The code works, but not the way it should operate. It uses two Geolocation instances and because the bindTo method the view periodically resets. I couldn't manage to extract the position variable of the Geolocation object. I've tried Geolocation.getPosition(), Geolocation.get('position') and Geolocation.p.position. Every one of them acted like a void method and returned an undefined value. I use Google Chrome version 35.0.1916.153 on a Debian Wheezy.

To complete the story, if I use only one Geolocation instance and bind it to the feature and the view objects, the point follows the center of the map with every change in the view.


The problem seems to be that the reading of the geolocation on load of the page doesn't work, probably because the geolocation isn't available yet. (especially if the browser asks the user first)

But you can read out the position inside a eventhandler. (tracking needs to be turned on). I call this eventhandler handler in the following examples.

Examples are: geolocation.on("change", handler ) (every time the geolocation object changes) or geolocation.once("change:position", handler ) (only one time the position of the geolocation object changes).

I made a fiddle that tracks your positions on the map: http://jsfiddle.net/simon_seyock/vbrzeakc/2/

  • Thank you for your response! You made almost the same thing with a simpler code which is very good. However you used an event listener with the feature object. My goal is to create a static code which updates the view and also creates a point to/on my location when the page loads. Then at least the view should not get updated. – Gabor Farkas Sep 6 '14 at 10:15
  • there might be a little problem: the geolocation might not be available when the page loads. but you could do it with a once instead of on. (i.e. geolocation.once('change', ... ) ) this fires the handler only the first time. – Simon Zyx Sep 6 '14 at 10:28
  • check: jsfiddle.net/simon_seyock/wo2p5ro3/1 try to comment the handler and uncomment the line above – Simon Zyx Sep 6 '14 at 10:32
  • i updated the answer – Simon Zyx Sep 6 '14 at 10:40
  • Very good. There wasn't a problem with tracking: true and view.setCenter(geolocation.getPosition()); also doesn't work. The geolocation.once event however works perfectly. I've added the create feature point function to the listener and it returned my preferred result with no errors. Thank you very much! – Gabor Farkas Sep 6 '14 at 10:42

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.