Openlayers 3 calculate distance

المشرف العام

Administrator
طاقم الإدارة
Question here concerning calculating distance in OpenLayers 3 (3.9.0).

I've got a function included in my project for measuring line/area features, basically lifted from the OpenLayers Examples. I'm using haversineDistance to calculate accurate lengths/areas, but keep getting an error preventing the numbers from being reported.

Basically, when I start to measure in the browser and console.log the measurements so I can see them, they appear to be reporting accurately; however, a js error in ol.js is preventing the measurements from being returned in the webpage so the viewer can see them. Error is 'Uncaught TypeError: Cannot read property 'length' of undefined'. Reports as occuring in ol.js on line 69.

My code for the function is:

var formatLengthFt = function(line) {var length_ft = 0;var coords = line.getCoordinates();for (var i = 0; i < coords.length; i++) { var c1 = ol.proj.transform(coords, 'EPSG:3857', 'EPSG:4326'); var c2 = ol.proj.transform(coords[i + 1], 'EPSG:3857', 'EPSG:4326'); length_ft += wgs84Sphere.haversineDistance(c1, c2); console.log(length_ft);}return length_ft;}Before employing haversineDistance, I simply used line.getLength(), but then realized the distances/areas were all inaccurate. So, tried the solution below with success, just can't report the numbers in the webpage. I've also tried using turf.js, with some level of success. At any rate, I just need a method for getting accurate distances/areas and would prefer to get the code above working correctly, but open to other solutions as well.

Thanks in advance for any help.



أكثر...
 
أعلى