1

I have code from openlayers that I cannot quite handle because I am so new for these kind of codes. My main goal is to measure area from n amount of lat/lon gps coordinates and convert the result into square kilometres.

I have adopted some code from the post here and it seems almost ready but I was not able to figure out how to use it with my test coordinates that are

var lats = [25.767368, 34.088808, 40.727093]
var lons = [-80.18930, -118.40612, -73.97864];

Which I need to use in code like this to get the area and finally convert it into square kilometres. Any help with this? Please make it simple as this is pretty much my first attempt on geagraphic coding.

var area = 0.0;
var len = ring.components && ring.components.length;

if (len > 2) {

var p1, p2;

for (var i=0; i<len-1; i++) {

    p1 = ring.components[i];
    p2 = ring.components[i+1];
    area += OpenLayers.Util.rad(p2.x - p1.x) *
        (2 + Math.sin(OpenLayers.Util.rad(p1.y)) +
        Math.sin(OpenLayers.Util.rad(p2.y)));

}

area = area * 6378137.0 * 6378137.0 / 2.0;

}
1

I write this html script for you from this formule, x and y are taken from your example.

Edit : This code was written for your previous question in which you are looking for a javascript code (This question has been deleted). I'm sorry if I'm off topic and I hope this code will help you anyway.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Titre</title>
    </head>
    <body>
    <script type="text/javascript" src="code.js">
    // Converts from degrees to radians.
Math.radians = function(degrees) {
  return degrees * Math.PI / 180;
};

x1 = 25.767368;
x2 = 34.088808;
x3 =40.727093;
x4 = ;
y1 = -80.18930;
y2 = -118.40612;
y3 = -73.97864;
y4 = ;

area = Math.radians(x2 - x1) * (2 + Math.sin(Math.radians(y1)) + Math.sin(Math.radians(y2))) + Math.radians(x3 - x2) * (2 + Math.sin(Math.radians(y2)) + Math.sin(Math.radians(y3)))+ rad(x4 - x3) * (2 + sin(rad(y3)) + sin(rad(y4)));;
alert(area);
    </script>
    </body>
</html>
  • Thanks for it anyway. I am trying to use that answer you mentioned above but when I try to add 4th point to it like x1 = 61.681229; x2 = 61.681489; x3 = 61.680372; x4 = 61.680644; y1 = 27.258061; y2 = 27.259472; y3 = 27.258775; y4 = 27.260191; I don't get the correct square meters. It should be 8000-11000 square meters from that area. – Defain Aug 6 '14 at 12:15
  • oh sorry I thought you need 3 points, i will rewrite it to let you have 4 points. :) – Leasye Aug 6 '14 at 12:17
  • Sorry for my part if I was unclear. I am looking for solution to work with n points. The 3 and 4 points are just testpoints I use because I know their correct area in square meters roughly. Of course is the formule is so I can just repeat the code I can write the for loops my self but the math needs to work :) – Defain Aug 6 '14 at 12:18
  • The formula is corrected but you have to fill x4 and y4 – Leasye Aug 6 '14 at 12:26
  • I added the points but I don't get the correct answer. I made jsfiddle about it in the guestion section with the desired answer and all the code. Any idea what is wrong? – Defain Aug 6 '14 at 12:28

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.