Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jensostertag/geocoding-util

Library to (reverse-)geocode addresses with the Nominatim API
https://github.com/jensostertag/geocoding-util

geocoding nominatim php

Last synced: about 2 months ago
JSON representation

Library to (reverse-)geocode addresses with the Nominatim API

Awesome Lists containing this project

README

        

# Geocoding-Util for PHP
This is a simple PHP library to (reverse-)geocode addresses with the Nominatim API.

> Legal Note: This library uses the [Nominatim](https://nominatim.org/) API. Please read the [Terms of Use](https://operations.osmfoundation.org/policies/nominatim/) before using it and comply with them.
>
> Data from OpenStreetMap is licensed under [ODbL](https://opendatacommons.org/licenses/odbl/).

> This library uses the [Curl-Adapter](https://github.com/JensOstertag/curl-adapter) library to send requests to the Nominatim API.
>
> The Curl-Adapter library is licensed under the [MIT License](https://github.com/JensOstertag/curl-adapter/blob/main/LICENSE-MIT).
> (c) 2023 Jens Ostertag

## Installation
To install this library, include it in your project using composer:
```json
{
"require": {
"jensostertag/geocoding-util": "1.0.0"
}
}
```

## Usage

Geocode an address to coordinates

The following example shows how to geocode an address to coordinates:
```php
setStreet("James-Franck-Ring")
->setHouseNumber("1")
->setCity("Ulm")
->setZipCode("89081")
->setCountry("Germany");
$coordinates = $geocoding->getCoordinates();
$lat = $coordinates["latitude"];
$lng = $coordinates["longitude"];
```
The above example will return the following coordinates:
```json
{
"latitude": 48.4253584,
"longitude": 9.956179
}
```

Reverse-geocode coordinates to an address

The following example shows how to reverse-geocode coordinates to an address:
```php
setCoordinates(48.4253584, 9.956179)
->toAddress();
$address = $geocoding->getAddress();
$street = $address["street"];
$houseNumber = $address["houseNumber"];
$city = $address["city"];
$zipCode = $address["zipCode"];
$country = $address["country"];
$formattedAddress = $geocoding->getFormattedAddress();
```
The above example will return the following address:
```json
{
"street": "James-Franck-Ring",
"houseNumber": null,
"city": "Ulm",
"zipCode": "89081",
"country": "Deutschland"
}
```
The formatted address will also be an array with two formatting options, inline and with `\n` line breaks:
```json
{
"inline": "James-Franck-Ring, 89081 Ulm, Deutschland (DE)",
"lineBreaks": "James-Franck-Ring\n89081 Ulm\nDeutschland (DE)"
}
```

Setting a custom user agent

You might want to set a custom user agent for your requests towards the Nominatim API to identify your application. To do that, use
```php

Setting a custom Nominatim API URL

The public Nominatim API is very limited in the amount of requests you can send. If you want to use your own Nominatim API instance, you can set a custom URL for the API. To do that, use
```php