struktal/struktal-geocoding-util 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

struktal/struktal-geocoding-util

最新稳定版本:1.1.0

Composer 安装命令:

composer require struktal/struktal-geocoding-util

包简介

PHP library for the Nominatim Geocoding API

README 文档

README

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

Legal Note: This library uses the Nominatim API. Please read the Terms of Use before using it and comply with them.

Data from OpenStreetMap is licensed under ODbL.

This library uses the Curl-Adapter library to send requests to the Nominatim API.

The Curl-Adapter library is licensed under the MIT License. (c) 2023 Struktal

Installation

To install this library, include it in your project using composer:

composer require struktal/struktal-geocoding-util

Usage

Geocode an address to coordinates

The following example shows how to geocode an address to coordinates:

<?php

use struktal\Geocoding\Geocoding;

$geocoding = new Geocoding();
$geocoding->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:

{
    "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

use struktal\Geocoding\Geocoding;
    
$geocoding = new Geocoding();
$geocoding->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:

{
    "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:

{
    "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

use struktal\Geocoding\Geocoding;

Geocoding::setUserAgent("MyApplication/1.0");

If you do not set a custom user agent, the default will be

Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0
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

use struktal\Geocoding\Geocoding;

Geocoding::setApiUrl("https://nominatim.mydomain.com");

统计信息

  • 总下载量: 1.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-only
  • 更新时间: 2025-06-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固