0
votes

I'm trying to figure how to use GPS coordinates in OpenLayers

A lot of older posts are referring to new OpenLayers, well that isn't available in 3.4 where everything is embedded in ol.

I figured this should be doable by something like this

var map = new ol.Map({
    target: 'map',
    layers: [
        new ol.layer.Tile({
            source: new ol.source.BingMaps({
                key: '',
                imagerySet: 'Aerial'
            })
        })
    ],
    //projection: "EPSG:4326",
    //displayProjection: "EPSG:3857",
    view: new ol.View({
        //center: [-13553864, 5918250],
        center: new ol.geom.Point(ol.proj.transform([58.4108600, 15.6215700], 'EPSG:4326', 'EPSG:3857')),
        zoom: 4
    })
});

But no, it sais TypeError: undefined is not a function

It is the new ol.geom.Point(ol.proj.transform([58.4108600, 15.6215700], 'EPSG:4326', 'EPSG:3857')) that is causing all the trouble, this is where almost any example say "use var lonlat = new OpenLayers.LonLat..." Thanks

2

2 Answers

2
votes

I was looking for this function too, and I have found it in some of the examples on their site.

You can use ol.proj.fromLonLat([19.062072, 47.473478]).

https://openlayers.org/en/latest/apidoc/module-ol_proj.html#.fromLonLat
Old link: http://openlayers.org/en/v3.13.1/apidoc/ol.proj.html#.fromLonLat

0
votes

The ol.View center property expect as a input an object of type ol.Coordinate. This is basically an Array with lon/lat in it (e.g. [-13553864, 5918250]) What you do is you use an Object of type ol.geom.Point as center property. Simple pass as center this:

ol.proj.transform([58.4108600, 15.6215700], 'EPSG:4326', 'EPSG:3857')

and it should work.