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

Overview

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.

Comments
  • Wrong marker position on smaller zoom numbers

    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.

    opened by virtualize 9
  • I'm able to draw shapes but base map doesn't show!

    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

    opened by JanYalda 7
  • Draw circle on map

    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.

    opened by virtualize 5
  • Automatic attribution in generated map corner

    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 !

    opened by cquest 5
  • Add tile server support

    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.

    opened by stephan-strate 3
  • Add support for curl options and fail curl on error in the TileLayer.

    Add support for curl options and fail curl on error in the TileLayer.

    Hi, Great little library you have! I've began testing it yesterday.

    As I was testing it, I noticed it didn't have much in the way of error checking on specifically the image retrieval. A good example is when you put a typo in the TileLayer construct for the url use "https://tilezzzzzz.openstreetmap.org/{z}/{x}/{y}.png". The image will get displayed with a grey background with nothing on it. Some people may want it to function like this.

    As such, made a construct option called failCurlOnError, and allowed curl option arrays in order to have more flexibility.

    In my use case, if there is a failure, I have the code falling back to another method.

    Thanks!

    opened by davetha 2
  • Projection of lat lon

    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

    opened by javiersansaloni 2
  • Update README with tile server usage policy

    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

    opened by mandrasch 1
Releases(0.5.0)
Owner
Franck Alary
Web and mobile app developer.
Franck Alary
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

Oscar Alderete 2 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

Dimitris Savvopoulos 119 Nov 29, 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

The League of Extraordinary Packages 1.3k Dec 27, 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,

IPinfo 171 Jan 2, 2023
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

phpMyAdmin 23 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

Geocoder 3.9k Jan 2, 2023
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

Marcus Jaschen 1.4k Dec 27, 2022
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

OpenCage GmbH 29 Nov 15, 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

PHP FFI 8 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

Eleven 47 Dec 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

delight.im 1.6k Dec 15, 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

ItsRealNise 7 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.

null 144 Dec 29, 2022
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

Garrett Johnson 20 Dec 14, 2022
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

PHP Geospatial, putting the Elephpant on your globe 56 Dec 29, 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

Jeremy Mikola 274 Dec 17, 2022
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

Franck Alary 6 Nov 29, 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

Oscar Alderete 2 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,

Erik Nielsen 455 Dec 30, 2022