Categories: Development

Leaflet map latitude and longitude marker with PHP

How to make a simple Leaflet map box with a marker at your set latitude and longitude values. Leaflet is a free, open-source javascript library for working with interactive maps.

This is what the final result will look like:

The marker location is set by lat and lon values in the URL, the example being map.php?lat=-37.818217&lon=144.956784

Gist link

These are the parts that make up map.php

Starting the page of with PHP which determines the latitude and longitude values, if they are not set it will state so and give an example link.

Getting values from a URL can be done in Javascript however I choose PHP because it will provide compatibility with another project.

<?php
if (isset($_GET['lat']) && isset($_GET['lon'])) {
    $lat = $_GET['lat'];
    $lon = $_GET['lon'];
} else {
    echo "<h1>Latitude and Longitude must be set in URL</h1>";
    echo "<p>Example: <a href='map.php?lat=-37.818217&lon=144.956784'>map.php?lat=-37.818217&lon=144.956784</a></p>";
    exit;
}
?>

Now the leaflet code, this goes before the closing </body> tag. PHP will output the lat and lon values, the map tiles are just regular Open street map style and the marker is not draggable.

$(function() {
    setLocation = [ <?php echo $lat; ?> , <?php echo $lon; ?> ];
    var map = L.map('MapBox').setView(setLocation, 14);
    L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    map.attributionControl.setPrefix(false);
    var marker = new L.marker(setLocation, {
        draggable: false
    });
    map.addLayer(marker);
})

This is the map.php page “boring” parts. It needs leaflet CSS, Jquery and leaflet JS to work. Then there is the custom CSS style for the map box.

<html lang="en">
<head>
    <title>Map</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.js"></script>
    <style>
        body {
            background: rgb(73, 234, 188);
        }

        .container {
            background-color: white;
            width: 25rem;
            position: absolute;
            top: 40%;
            left: 50%;
            transform: translate(-50%, -50%);
            padding: 1.4rem;
            box-shadow: 1px 5px 5px 0 rgba(0, 0, 0, 0.20);
            border-radius: 0.2rem;
            box-sizing: border-box;
        }

        .container * {
            box-sizing: inherit;
            font-size: inherit;
        }

        .container #MapBox {
            margin-bottom: 0.75rem;
        }
    </style>
</head>
<body>
<div class="container">
    <div id="MapBox" style="height: 21rem"></div>
</div>

Thats it, this will make a map box with a market placed for the latitude and longitude set via the URL.

Gist link

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago