22

I am looking for an accurate algorithm or a service to calculate surface area on earth where points are calculated on the basis of GPS Coordinates.

I am using Google Map Api version 3 and are drawing polygons based on recorded coordinates but I don't think the standard ways of calculating area of the polygon will take into account of slopes(hills). Do I need to work on contours for such thing?

Are there any third party services may be ArcGis or some other that takes into account of slopes as well.

Kunal
  • 1,913
  • 6
  • 29
  • 45

2 Answers2

49

Yes, this is definitely possible. There is a quick tutorial with example code here:

https://web.archive.org/web/20130301120148/http://geojason.info/demos/line-length-polygon-area-google-maps-v3

The relevant part is this:

google.maps.geometry.spherical.computeArea(yourPolygon.getPath());

Official documentation:

http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical

Brad
  • 159,648
  • 54
  • 349
  • 530
  • will this API take into account the elevation of the land?. I will be getting coordinates from a GPS device. – Kunal Jan 28 '12 at 22:07
  • 1
    No, it assumes a perfectly smooth spherical earth. Sounds like what you really want is the surface area of a digital elevation model. This can be computed by the r.surf.area function in GRASS GIS, but if you want to know more you should probably ask on gis.stackexchange.com – Spacedman Jan 28 '12 at 23:29
  • @Brad hey i opened the first link but we got page not found. can u plz send me other related link? – Google Jul 23 '14 at 12:50
  • 2
    @Google You're Google, can't you find it? :-D Sorry, I don't know of a replacement link. I'll leave the link up in case anyone wants to find it on archive.org. In any case, that line is what you're looking for, and you can read more about it in the documentation. – Brad Jul 23 '14 at 13:26
  • @Brad ya i am google but sometime google also confuze for that.bdw i want to draw line path between two places. – Google Jul 24 '14 at 04:22
  • @Brad the geojason.info link is broken – knownasilya Mar 10 '15 at 16:03
  • don't forget the include the geometry library when loading google maps (the call to `computeArea` wasn't working here initially) -- see [here](https://stackoverflow.com/a/40348347/426790) – Greg Sadetsky Apr 15 '19 at 02:05
11

Brad's answer

google.maps.geometry.spherical.computeArea(yourPolygon.getPath());

is correct, but beware, it only applies for polygons which do not self-intersect. When the polygon starts to self-intersect, things are going horribly wrong. You can try it with the link Brad gave http://geojason.info/demos/line-length-polygon-area-google-maps-v3/ . Just draw 4-5 intersecting lines and start playing with the vertices. The area calculation will definitely seem wrong.

If you're not convinced, here is an example:

   var map;

    google.maps.visualRefresh = true;

    google.maps.event.addDomListener(window, 'load', initialize);

    function initialize() {
        var mapOptions = {
            center : new google.maps.LatLng(55.874, -4.287),
            zoom : 16,
            mapTypeId : google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions)

        drawExample();
    }

    function drawExample() {
        var pathLeft = [new google.maps.LatLng(55.874, -4.292),
            new google.maps.LatLng(55.875, -4.292),
            new google.maps.LatLng(55.875, -4.290),
            new google.maps.LatLng(55.876, -4.290),
            new google.maps.LatLng(55.876, -4.291),
            new google.maps.LatLng(55.874, -4.291)]

        var polygonLeft = new google.maps.Polygon({
            path : pathLeft,
            map: map
        });

        var areaLeft = google.maps.geometry.spherical.computeArea(polygonLeft.getPath());

        var pathRight = [new google.maps.LatLng(55.874, -4.282),
            new google.maps.LatLng(55.875, -4.282),
            new google.maps.LatLng(55.875, -4.280),
            new google.maps.LatLng(55.8753, -4.2807),
            new google.maps.LatLng(55.876, -4.281),
            new google.maps.LatLng(55.874, -4.281)]

        var polygonRight = new google.maps.Polygon({
            path : pathRight,
            map: map
        });

        var areaRight = google.maps.geometry.spherical.computeArea(polygonRight.getPath());

        console.log("areaLeft: " + areaLeft + "\nareaRight: " + areaRight);
    }

The bug report http://code.google.com/p/gmaps-api-issues/issues/detail?id=5624&colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal&start=700#makechanges

Kostis
  • 1,593
  • 15
  • 16
  • can you please a fiddle for the code you are showing in this answer. This will be more helpful. – Pawan Gupta Dec 02 '19 at 10:51
  • @PawanGupta here's a jsfiddle created by someone in the bug report http://jsfiddle.net/kaiser/VjQjg/ Check your console and you'll find the output with the wrong calculations – Kostis Dec 10 '19 at 18:08