Skip to content

Instantly share code, notes, and snippets.

@Rotron
Forked from magicbug/latlong_to_locator.php
Created June 6, 2019 08:40
Show Gist options
  • Save Rotron/0e52cc865493f766ab4695265afc03e9 to your computer and use it in GitHub Desktop.
Save Rotron/0e52cc865493f766ab4695265afc03e9 to your computer and use it in GitHub Desktop.

Revisions

  1. @magicbug magicbug renamed this gist Apr 15, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @magicbug magicbug renamed this gist Apr 15, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @magicbug magicbug revised this gist Apr 15, 2015. No changes.
  4. @magicbug magicbug created this gist Apr 15, 2015.
    46 changes: 46 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?php

    // Example Lat/Long aka IO91js
    $latitude = "51.756435";
    $longitude = "-1.246042";

    echo latlong_to_locator($latitude, $longitude);

    function latlong_to_locator ($latitude, $longitude) {

    /*
    Converts WGS84 coordinates into the corresponding Maidenhead Locator
    Inputs:-
    $latitude
    $longitude
    */

    if ($longitude >= 180 || $longitude <= -180) {
    return "Longitude Value Incorrect";
    }

    if ($latitude >= 90 || $latitude <= -90) {
    return "Latitude Value Incorrect";
    }

    $longitude += 180;
    $latitude += 90;


    $letterA = ord('A');
    $numberZero = ord('0');

    $locator = chr($letterA + intval($longitude / 20));
    $locator .= chr($letterA + intval($latitude / 10));
    $locator .= chr($numberZero + intval(($longitude % 20) / 2));
    $locator .= chr($numberZero + intval($latitude % 10));
    $locator .= chr($letterA + intval(($longitude - intval($longitude / 2) * 2) / (2 / 24)));
    $locator .= chr($letterA + intval(($latitude - intval($latitude / 1) * 1 ) / (1 / 24)));

    return $locator;

    }

    ?>