php code to find the elevation from latitude and longitude

To find the elevation from latitude and longitude in PHP, you can use various APIs that provide elevation data based on geographic coordinates. One popular API for this purpose is the Google Maps Elevation API. Here's an example of how you can use it in PHP:


function getElevation($latitude, $longitude) {
    // Replace 'YOUR_API_KEY' with your actual Google Maps API key
    $apiKey = 'YOUR_API_KEY';

    // API endpoint for Google Maps Elevation API
    $apiEndpoint = '';

    // Create the request URL
    $requestUrl = $apiEndpoint . '?locations=' . $latitude . ',' . $longitude . '&key=' . $apiKey;

    // Make a request to the Google Maps Elevation API
    $response = file_get_contents($requestUrl);

    // Decode the JSON response
    $data = json_decode($response, true);

    // Check if the request was successful
    if ($data['status'] === 'OK') {
        // Extract the elevation from the response
        $elevation = $data['results'][0]['elevation'];
        return $elevation;
    } else {
        // Handle the error (e.g., display an error message)
        return 'Error: ' . $data['status'];

// Example usage
$latitude = 37.7749; // Replace with the actual latitude
$longitude = -122.4194; // Replace with the actual longitude

$elevation = getElevation($latitude, $longitude);

if (!is_numeric($elevation)) {
    echo 'Error: Unable to retrieve elevation - ' . $elevation;
} else {
    echo 'Elevation at (' . $latitude . ', ' . $longitude . ') is ' . $elevation . ' meters.';


Make sure to replace 'YOUR_API_KEY' with your actual Google Maps API key. Note that the Google Maps Elevation API has some usage limitations, and you may need to enable billing on your Google Cloud Platform account.

Keep in mind that there are other elevation APIs available, and the usage may vary. Always check the documentation of the specific API you choose for any authentication requirements, usage limits, and terms of service.

You can create a simple HTML form to take input for latitude and longitude and then use PHP to process that input and display the elevation. Here's an example:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elevation Finder</title>

    <h2>Find Elevation</h2>

    <form action="" method="post">
        <label for="latitude">Latitude:</label>
        <input type="text" name="latitude" id="latitude" required>

        <label for="longitude">Longitude:</label>
        <input type="text" name="longitude" id="longitude" required>

        <button type="submit" name="submit">Find Elevation</button>

    // Check if the form is submitted
    if (isset($_POST['submit'])) {
        // Get latitude and longitude from the form
        $latitude = isset($_POST['latitude']) ? floatval($_POST['latitude']) : 0;
        $longitude = isset($_POST['longitude']) ? floatval($_POST['longitude']) : 0;

        // Call the getElevation function
        $elevation = getElevation($latitude, $longitude);

        // Display the result
        if (!is_numeric($elevation)) {
            echo '<p>Error: Unable to retrieve elevation - ' . $elevation . '</p>';
        } else {
            echo '<p>Elevation at (' . $latitude . ', ' . $longitude . ') is ' . $elevation . ' meters.</p>';


This example includes a simple HTML form with input fields for latitude and longitude. When the form is submitted, the PHP code processes the input and calls the getElevation function to retrieve and display the elevation.

Related Articles

- All From ChatGPT