PHP library to easily get static image from OpenStreetMap (OSM) with markers and lines.

Last update: Jul 28, 2022

PHP OpenStreetMap Static API

PHP library to easily get static image from OpenStreetMap with markers and lines.

Supporting

Star this repository to support this project. You will contribute to increase the visibility of this library 🙂

Installation

Install this library easily with composer :

composer require dantsu/php-osm-static-api

How to use

Generate OpenStreetMap static image with marker and line :

use \DantSu\OpenStreetMapStaticAPI\OpenStreetMap;
use \DantSu\OpenStreetMapStaticAPI\LatLng;
use \DantSu\OpenStreetMapStaticAPI\Line;
use \DantSu\OpenStreetMapStaticAPI\Markers;

\header('Content-type: image/png');
(new OpenStreetMap(new LatLng(44.351933, 2.568113), 17, 600, 400))
    ->addMarkers(
        (new Markers(__DIR__ . '/resources/marker.png'))
            ->setAnchor(Markers::ANCHOR_CENTER, Markers::ANCHOR_BOTTOM)
            ->addMarker(new LatLng(44.351933, 2.568113))
            ->addMarker(new LatLng(44.351510, 2.570020))
            ->addMarker(new LatLng(44.351873, 2.566250))
    )
    ->addLine(
        (new Line('FF0000', 2))
            ->addPoint(new LatLng(44.351172, 2.571092))
            ->addPoint(new LatLng(44.352097, 2.570045))
            ->addPoint(new LatLng(44.352665, 2.568107))
            ->addPoint(new LatLng(44.352887, 2.566503))
            ->addPoint(new LatLng(44.352806, 2.565972))
            ->addPoint(new LatLng(44.351517, 2.565672))
            ->addPoint(new LatLng(44.351172, 2.571092))
    )
    ->getImage()
    ->displayPNG();

Exported OpenStreetMap image

Documentation

Class Description
BoundingBox DantSu\OpenStreetMapStaticAPI\BoundingBox define the bounding box of the static map.
LatLng DantSu\OpenStreetMapStaticAPI\LatLng define latitude and longitude for map, lines, markers.
Line DantSu\OpenStreetMapStaticAPI\Line draw line on the map.
Markers DantSu\OpenStreetMapStaticAPI\Markers display markers on the map.
OpenStreetMap DantSu\OpenStreetMapStaticAPI\BoundingBox define the bounding box of the static map.
XY DantSu\OpenStreetMapStaticAPI\XY define X and Y pixel position for map, lines, markers.

Contributing

Please fork this repository and contribute back using pull requests.

Any contributions, large or small, major features, bug fixes, are welcomed and appreciated but will be thoroughly reviewed.

GitHub

https://github.com/DantSu/php-osm-static-api
Comments
  • 1. Wrong marker position on smaller zoom numbers

    Thank you very much for this fine library.

    It seems like a marker it getting shifted up the smaller the zoom is, i.e. for zoom 12 the marker position seems good. If I decrease the zoom stepwise to smaller numbers, the marker will be positioned above (and more left) of the given coordinates.

    In the following images the zoom is decreased from 12 to 8, 6 and 4. At zoom 4 the marker is almost at germanys norther border, but the coordinates point to a place way south.

    db7b1e5f-a6db-4942-9ee7-f1f59f245842 efddea52-d2e2-461e-849d-865ff37e0303 f68da997-483f-48b0-9767-d4921d17a078 4eac9f48-f4e7-4e88-920f-a14867c77152

    My setup is rather simple

    $tempFilePath = tempnam(sys_get_temp_dir(), 'osm');
    $markerPath = $this->parameterBag->get('kernel.project_dir').'/assets/images/marker-black.png';
    
    $markers = (new Markers($markerPath))
        ->setAnchor(Markers::ANCHOR_CENTER, Markers::ANCHOR_BOTTOM)
        ->addMarker(new LatLng($lat, $lng));
    
    $map = (new OpenStreetMap(new LatLng($lat, $lng), $zoom, 640, 640))
        ->addMarkers($markers);
    
    $map->getImage()->savePNG($tempFilePath);
    

    I had a quick look at Markers::draw() but could not find the problem.

    Edit: The marker anchor position does not seem to have anything to do with the misplacement.

    Reviewed by virtualize at 2022-04-01 17:28
  • 2. I'm able to draw shapes but base map doesn't show!

    Initially when I first used this package it worked just fine with any coordinates, but I had to update my php version to 8 to accommodate other needs of my project and I'm not sure if that has anything to do with this but it now has stopped working like before mainly that the base map(tileServer?) doesn't show up anymore but the drawing of the spatial shapes still functions and the display/save options work as well!

    Here's how it looks: test

    Reviewed by JanYalda at 2022-06-22 01:43
  • 3. Draw circle on map

    Would it be possible to draw a circle on the map, like in this example:

    Screenshot 2022-05-17 at 18 10 05

    I saw that your Image class in php-image-editor has a drawCircle method, but I'm not sure how to use it to draw on a map image. Thanks for any pointers.

    Reviewed by virtualize at 2022-05-17 16:20
  • 4. Automatic attribution in generated map corner

    OpenStreetMap based maps need attribution, see: https://openstreetmap.org/

    It would be nice to add it automatically in the corner of the generated PNG.

    Merci d'avance !

    Reviewed by cquest at 2021-09-08 16:13
  • 5. Add tile server support

    First of all: Thanks for this amazing library. I have added support for tile servers other than OpenStreetMaps. I made sure, that its backwards compatible.

    The update for the technical documentation is missing, maybe you can run the phpDocumentor.

    Maybe you can also give me some instructions on how you want to have the documentation for the new feature in the readme file.

    Reviewed by stephan-strate at 2022-05-28 14:39
  • 6. Projection of lat lon

    Great Job!

    What is the projection of the lat lon variables?

    I have tried to put my town (Burriana/Castellon/Spain) and I see my marker on Africa

    Thank you

    Reviewed by javiersansaloni at 2022-01-24 17:34
  • 7. Update README with tile server usage policy

    Hey, great project! I researched the topic of static maps as well, thought about using this approach for a wordpress gutenberg block maybe.

    During my research I stumbled about the fact, that Open Streetmap data is free for everyone to use, but tile servers are run by donations and have restrictions (It is also possible to setup own tile server or use commercial hosted services).

    Just added this line to make sure you give users all the necessary information on restrictions. :-)

    Best regards

    Reviewed by mandrasch at 2021-11-06 09:32
New WordPress plugin to render a map with multiple markers using the open source maps provider OpenStreetMap

markers-on-openstreetmap New WordPress plugin to render a map with multiple markers using the open source maps provider OpenStreetMap A friend of mine

Oct 25, 2021
A Laravel package to get the user's country using the ip.

Laravel IP Service Tries to guess the country code of the client, using his IP. Installation Download and import the ip database from ip2nation.com Us

Apr 12, 2022
Geo-related tools PHP 5.4+ library built atop Geocoder and React libraries

Geotools Geotools is a PHP geo-related library, built atop Geocoder and React libraries. Features Batch geocode & reverse geocoding request(s) in seri

Aug 7, 2022
Official PHP library for IPinfo (IP geolocation and other types of IP data)

This is the official PHP client library for the IPinfo.io IP address API, allowing you to lookup your own IP address,

Aug 14, 2022
ESRI ShapeFile library for PHP

shapefile ShapeFile library for PHP Features Currently the 2D and 3D variants except MultiPatch of the ShapeFile format as defined in https://www.esri

Jun 29, 2022
The most featured Geocoder library written in PHP.

Geocoder Important: You are browsing the documentation of Geocoder 4.x. Documentation for version 3.x is available here: Geocoder 3.x documentation. D

Aug 9, 2022
Simple Yet Powerful Geo Library for PHP

phpgeo - A Simple Geo Library for PHP phpgeo provides abstractions to geographical coordinates (including support for different ellipsoids) and allows

Jul 29, 2022
PHP library to access the OpenCage geocoding API
PHP library to access the OpenCage geocoding API

OpenCage Geocoding API Library for PHP A PHP library to use the OpenCage geocoding API. Build Status / Code Quality Overview This library attempts to

Jun 20, 2022
PHP library for determining the physical location of binaries

Bin Locator Library for searching binary files in the operating system. Requirements PHP >= 7.4 Installation Library is available as composer reposito

Jul 27, 2022
GeoSpatial integration on Laravel 5.2+ that supports MySQL and PostgreSQL.

Features GeoSpatial integration on Laravel 5.2+: Create geospatial columns using Schema and migrations Save and retrieve geospatial attributes using d

May 22, 2022
Free database of geographic place names and corresponding geospatial data

FreeGeoDB Free database of geographic place names and corresponding geospatial data Entities airports cities countries (admin-0) lakes ports railroads

Jul 27, 2022
Use: [i] to share item and name in hand, [coor] to share you current coordinates

General Now you can share your Coordinates and Item with Prefix Example if you type [i] in message, later it will be automatically replaced into the n

Oct 15, 2021
Generate and display maps without external services or compromising on privacy.
Generate and display maps without external services or compromising on privacy.

Generate and display maps without external services or compromising on privacy.

Jun 24, 2022
PHP extension for geospatial rendering with Mapnik
PHP extension for geospatial rendering with Mapnik

PHP7 Mapnik Introduction This project is an extension for PHP 7 that enables geospatial rendering with Mapnik. Create tile generation scripts, dynamic

Dec 17, 2021
PHP Extension to handle common geospatial functions.

geospatial - PHP Geospatial Extension PHP Extension to handle common geospatial functions. The extension currently has implementations of the Haversin

Aug 11, 2022
GeoJSON implementation for PHP

GeoJson PHP Library This library implements the GeoJSON format specification. The GeoJson namespace includes classes for each data structure defined i

Aug 4, 2022
PHP library to easily get static image from French Cadastral Government map with markers and lines.
PHP library to easily get static image from French Cadastral Government map with markers and lines.

PHP French Cadastral Map Static API PHP library to easily get static image from French Cadastral Government map with markers and lines. Map source : c

May 24, 2022
New WordPress plugin to render a map with multiple markers using the open source maps provider OpenStreetMap

markers-on-openstreetmap New WordPress plugin to render a map with multiple markers using the open source maps provider OpenStreetMap A friend of mine

Oct 25, 2021
Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file, move it to a new directory, and returns the new source for the image.

NO LONGER MAINTAINED!!! Image Cache v. 1.0.0 Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file,

Aug 11, 2022