定制 arraypress/mapsco-geocoding 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

arraypress/mapsco-geocoding

Composer 安装命令:

composer require arraypress/mapsco-geocoding

包简介

A PHP library for integrating with the Maps.co Geocoding API in WordPress, providing both forward and reverse geocoding capabilities with WordPress transient caching.

README 文档

README

A WordPress library for Maps.co Geocoding API integration with smart caching and comprehensive location data handling.

Installation

Install via Composer:

composer require arraypress/mapsco-geocoding

Requirements

  • PHP 7.4 or later
  • WordPress 6.2.2 or later
  • Maps.co API key

Basic Usage

use ArrayPress\MapsCo\Geocoding\Client;

// Initialize with your API key
$client = new Client( 'your-api-key-here' );

// Forward geocoding (address to coordinates)
$location = $client->geocode( '1600 Pennsylvania Avenue NW, Washington, DC' );

// Reverse geocoding (coordinates to address)
$location = $client->reverse_geocode( 38.8977, -77.0365 );

Available Methods

Client Methods

// Initialize client with options
$client = new Client(
	'your-api-key-here',  // API key
	'json',              // Response format (optional, default: 'json')
	true,               // Enable caching (optional, default: true)
	604800             // Cache duration in seconds (optional, default: 1 week)
);

// Forward geocoding
$location = $client->geocode( '1600 Pennsylvania Avenue NW, Washington, DC' );

// Reverse geocoding
$location = $client->reverse_geocode( 38.8977, - 77.0365 );

// Cache management
$client->clear_cache( 'address_key' );  // Clear specific cache
$client->clear_cache();                 // Clear all cached geocoding data

Location Methods

// Get coordinates
$lat = $location->get_latitude();   // Returns: 38.8977
$lon = $location->get_longitude();  // Returns: -77.0365

// Get formatted address
$display_name = $location->get_display_name();
// Returns: "White House, 1600, Pennsylvania Avenue Northwest, Washington, DC 20500"

// Get address components
$house_number = $location->get_house_number();  // Returns: "1600"
$street       = $location->get_street();             // Returns: "Pennsylvania Avenue Northwest"
$city         = $location->get_city();                // Returns: "Washington"
$state        = $location->get_state();              // Returns: "District of Columbia"
$postcode     = $location->get_postcode();        // Returns: "20500"
$country      = $location->get_country();          // Returns: "United States"
$country_code = $location->get_country_code(); // Returns: "US"
$borough      = $location->get_borough();          // Returns: "Ward 2"

// Get OpenStreetMap data
$place_id = $location->get_place_id();   // Returns: OSM place ID
$osm_type = $location->get_osm_type();   // Returns: "way"
$osm_id   = $location->get_osm_id();       // Returns: OSM ID

// Get location type information
$class = $location->get_class();  // Returns: e.g., "office"
$type  = $location->get_type();    // Returns: e.g., "government"

// Get importance ranking
$importance = $location->get_importance();  // Returns: float value

// Get license information
$license = $location->get_license();  // Returns: license text

// Get bounding box
if ( $location->has_bounding_box() ) {
	$bbox = $location->get_bounding_box();
	// Returns: [
	//     'min_lat' => float,
	//     'max_lat' => float,
	//     'min_lon' => float,
	//     'max_lon' => float
	// ]
}

Raw Data Access

// Get complete raw data array
$raw_data = $location->get_raw_data();

Response Format Examples

Forward Geocoding Response

[
    'place_id' => 308584984,
    'licence' => 'Data © OpenStreetMap contributors, ODbL 1.0.',
    'osm_type' => 'way',
    'osm_id' => 238241022,
    'lat' => '38.897699700000004',
    'lon' => '-77.03655315',
    'display_name' => 'White House, 1600, Pennsylvania Avenue Northwest, Ward 2, Washington, District of Columbia, 20500, United States',
    'class' => 'office',
    'type' => 'government',
    'importance' => 1.05472115416811,
    'boundingbox' => [
        '38.8974908',
        '38.897911',
        '-77.0368537',
        '-77.0362519'
    ]
]

Reverse Geocoding Response

[
    'place_id' => 308584984,
    'licence' => 'Data © OpenStreetMap contributors, ODbL 1.0.',
    'osm_type' => 'way',
    'osm_id' => 238241022,
    'lat' => '38.897699700000004',
    'lon' => '-77.03655315',
    'display_name' => 'White House, 1600, Pennsylvania Avenue Northwest, Ward 2, Washington, District of Columbia, 20500, United States',
    'address' => [
        'office' => 'White House',
        'house_number' => '1600',
        'road' => 'Pennsylvania Avenue Northwest',
        'borough' => 'Ward 2',
        'city' => 'Washington',
        'state' => 'District of Columbia',
        'postcode' => '20500',
        'country' => 'United States',
        'country_code' => 'us'
    ],
    'boundingbox' => [
        '38.8974908',
        '38.897911',
        '-77.0368537',
        '-77.0362519'
    ]
]

Error Handling

The library uses WordPress's WP_Error for error handling:

$location = $client->geocode( 'invalid address' );

if ( is_wp_error( $location ) ) {
    echo $location->get_error_message();
    // Output: "No results found for the provided address"
}

Common error cases:

  • Invalid address
  • Invalid coordinates
  • Invalid API key
  • API request failure
  • No results found
  • Invalid response format

Contributions

Contributions to this library are highly appreciated. Raise issues on GitHub or submit pull requests for bug fixes or new features. Share feedback and suggestions for improvements.

License: GPLv2 or later

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

arraypress/mapsco-geocoding 适用场景与选型建议

arraypress/mapsco-geocoding 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「geocoding」 「address」 「wordpress」 「geolocation」 「maps」 「location」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 arraypress/mapsco-geocoding 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 arraypress/mapsco-geocoding 我们能提供哪些服务?
定制开发 / 二次开发

基于 arraypress/mapsco-geocoding 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2024-12-17