Exploring the Onwater.io Public API

Onwater.io provides a free public API for determining if a given point of latitude and longitude is on water or on land. This API is useful for a variety of applications, from designing shipping routes to aquatic ecology research. In this article, we'll explore how to use the Onwater.io API, and provide example code in JavaScript.

Using the Onwater.io Public API

First, we need to obtain an API key from Onwater.io. This is a simple process – navigate to the Onwater.io API page, click the "Get Started" button, and follow the instructions to sign up for an API key.

The basic API call takes one parameter – a pair of latitude and longitude coordinates in decimal degrees. The API returns a JSON object that contains the location's "water" status: either "water" or "land". Here's an example of the API call structure:

https://api.onwater.io/api/v1/results/<lat>,<lon>/?access_token=<your_access_token>

Example JavaScript Code Using the Onwater.io API

Let's examine how to use the Onwater.io API in JavaScript. In this example, we'll use the Fetch API to make the API call and return the response.

const latitude = 40.7128;
const longitude = -74.0060;
const accessToken = 'your_access_token';

fetch(`https://api.onwater.io/api/v1/results/${latitude},${longitude}/?access_token=${accessToken}`)
  .then(response => response.json())
  .then(data => console.log(data));

In this example code, we pass the latitude and longitude as variables, and substitute them into the API call using template literals. We also include the access token as a query parameter.

Once we have the response from Onwater.io, we can handle the data as needed. Here's an example of how to handle the response to display whether a location is on land or on water:

const latitude = 40.7128;
const longitude = -74.0060;
const accessToken = 'your_access_token';

fetch(`https://api.onwater.io/api/v1/results/${latitude},${longitude}/?access_token=${accessToken}`)
  .then(response => response.json())
  .then(data => {
    if (data.water) {
      console.log("This location is in water");
    } else {
      console.log("This location is on land");
    }
  });

In conclusion, the Onwater.io public API is a useful and easy-to-use service for determining a location's water status. With our example code in JavaScript, you can quickly and easily integrate the API into your own web applications and projects.

Related APIs