Posted on

Get average elevation of rectangle in google maps

One of the services providing earth elevation is “The Google Elevation API” <>. There are lot of tutorials describing the way to catch the z-value of point or line. In this text we would like to show one possibility to get average elevation of a rectangle.

We will start with html code


Then we would like to build a default map and some interaction for making our rectangle. We will also be able to specify the number of points on X and Y axis to calculate the average from.

For interaction we will use google maps API, that is initialized this way:


The rectangle will be created with mouseclick event:


…then makeRectangle(e) will look :


At this point we are able to create rectangle on click and change its position and size. With next function calculatePoints() we will cover this rectangle with number of points we defined in html- with two <input type=”number”> and their  xValue and yValue.


Function getElevation(mPosition, x, y, valX, valY) will be launched for all points. We pass all the arguments to be able to indicate last point to avoid callbacking.

As a result we will get something like this













You can try this code on jsfiddle 

Leave a Reply