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 badges2 Answers
Reset to default 12Not 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:
var lonlat = ol.proj.toLonLat(evt.coordinate);
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) { }