最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - latitude and longitude of current position in openlayer - Stack Overflow

programmeradmin4浏览0评论

I am trying to get the current location latitude and longitude values. is there any method to get lat and long values? I am trying to put maker by getting latitude and longitude value of current position.

I am trying to get the current location latitude and longitude values. is there any method to get lat and long values? I am trying to put maker by getting latitude and longitude value of current position.

Share Improve this question edited Aug 31, 2017 at 6:56 v. josh asked Aug 30, 2017 at 10:54 v. joshv. josh 1191 gold badge2 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Not sure exactly what you're looking for but here are a couple of ways to get lat long based on mouse click or location:

Mouse Click Lat and Lon:

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;
});

Mouse move/location lat and long:

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

Not exactly sure what "getting latitude and longitude value of current position." means in your post

You can use either:

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

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

And get it as: alert("latitude : " + lonlat[1] + ", longitude : " + lonlat[0]);

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

发布评论

评论列表(0)

  1. 暂无评论