0

How, in JavaScript and OpenLayers, can I pass my WKT point string to a method and get back an object that will parse the longitude and latitude into properties for me?

'POINT (XXX YYY)'
to
SomeObject.longitude
and 
SomeOjbect.latitude
  • 2
    use ol.format.WKT#readFeature – Jose Hermosilla Rodrigo Jul 5 '17 at 14:49
  • I can't find longitude and latitude (debugger on line 13) in the feature object...jsfiddle.net/5hdu8bdr – Rod Jul 5 '17 at 15:33
  • To get a list of longitude/latitude from the feature just: feature.getGeometry().getCoordinates() – Jose Hermosilla Rodrigo Jul 5 '17 at 16:00
  • 1
    the coordinates look different 'POINT(10.689 -25.092)' vs [1189894.0370893013, -2887048.988883849]? – Rod Jul 5 '17 at 17:21
  • That's because you are converting the coordinates from 4326 to web mercator projection. You can either transform your coordinates (just for use/print) using ol.proj.transform or use EPSG:4326 as your map projection and let the wkt parser dont transform anything (get ride of featureProjection property). – Jose Hermosilla Rodrigo Jul 5 '17 at 17:48
1

Try grabbing the coordinates with a regex:

wktString = 'POINT(10.689 -25.092)';
coordFinder = /\(\s?(\S+)\s+(\S+)\s?\)/g;
allMatches = coordFinder.exec(wktString);
lon = parseFloat(allMatches[1]);
lat = parseFloat(allMatches[2]);
  • This is similar to what I was doing except I was using substring. – Rod Jul 5 '17 at 18:09

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.