arraypress/google-distance-matrix 问题修复 & 功能扩展

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

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

arraypress/google-distance-matrix

Composer 安装命令:

composer require arraypress/google-distance-matrix

包简介

A PHP library for integrating with the Google Distance Matrix API in WordPress, providing travel distance and time calculations between multiple origins and destinations. Features WordPress transient caching and WP_Error support.

README 文档

README

A PHP library for integrating with the Google Distance Matrix API in WordPress. This library provides robust distance and time calculations between multiple origins and destinations with support for WordPress transient caching and WP_Error handling.

Features

  • 📊 Distance Calculations: Calculate distances between multiple origins and destinations
  • ⏱️ Travel Times: Get accurate travel duration with traffic considerations
  • 🚗 Multiple Modes: Support for driving, walking, cycling, and transit modes
  • 🌍 International: Support for distance calculations worldwide
  • 📏 Unit Flexibility: Support for both metric and imperial measurements
  • 🔄 Response Parsing: Clean response object for easy data access
  • WordPress Integration: Native transient caching and WP_Error support
  • 🛡️ Type Safety: Full type hinting and strict types
  • 🚦 Traffic Data: Optional real-time traffic consideration
  • 🗺️ Route Options: Support for route restrictions and preferences
  • 🔗 Fluent Interface: Chainable methods for setting options

Requirements

  • PHP 7.4 or later
  • WordPress 6.7.1 or later
  • Google Distance Matrix API key

Installation

Install via Composer:

composer require arraypress/google-distance-matrix

Basic Usage

use ArrayPress\Google\DistanceMatrix\Client;

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

// Using fluent interface
$result = $client
	->set_mode( 'driving' )
	->set_units( 'imperial' )
	->set_avoid( 'tolls' )
	->calculate( 'New York, NY', 'Boston, MA' );

if ( ! is_wp_error( $result ) ) {
	// Get distance and duration
	$distance = $result->get_formatted_distance();
	$duration = $result->get_formatted_duration();

	echo "Distance: {$distance}\n";
	echo "Duration: {$duration}\n";

	// Get raw values in meters/seconds
	$meters  = $result->get_distance_meters();
	$seconds = $result->get_duration_seconds();
}

Setting Default Options

// Set defaults for multiple calculations
$client
	->set_mode( 'driving' )
	->set_units( 'imperial' )
	->set_language( 'en' );

// Use defaults
$result1 = $client->calculate( 'New York, NY', 'Boston, MA' );

// Override specific options
$result2 = $client->calculate(
	'New York, NY',
	'Boston, MA',
	[ 'mode' => 'transit' ]
);

// Reset to default options
$client->reset_options();

Multiple Origins/Destinations

$origins = [
	'San Francisco, CA',
	'Los Angeles, CA'
];

$destinations = [
	'Las Vegas, NV',
	'Phoenix, AZ',
	'San Diego, CA'
];

$result = $client
	->set_mode( 'driving' )
	->set_units( 'imperial' )
	->calculate( $origins, $destinations );

if ( ! is_wp_error( $result ) ) {
	$distances = $result->get_all_distances();
	foreach ( $distances as $route ) {
		echo "From: {$route['origin']}\n";
		echo "To: {$route['destination']}\n";
		echo "Distance: {$route['distance']['text']}\n";
		echo "Duration: {$route['duration']['text']}\n\n";
	}
}

Available Options

Travel Modes

const VALID_MODES = [
    'driving',
    'walking',
    'bicycling',
    'transit'
];

Units

const VALID_UNITS = [
    'metric',
    'imperial'
];

Avoid Options

const VALID_AVOID = [
    'tolls',
    'highways',
    'ferries'
];

Traffic Models

const VALID_TRAFFIC_MODELS = [
    'best_guess',
    'pessimistic',
    'optimistic'
];

Default Options

const DEFAULT_OPTIONS = [
    'mode' => 'driving',
    'units' => 'metric',
    'language' => 'en'
];

Working with Options Array

$result = $client->calculate(
    'New York, NY',
    'Boston, MA',
    [
        'mode' => 'driving',           // driving, walking, bicycling, transit
        'units' => 'imperial',         // metric or imperial
        'avoid' => 'tolls',           // tolls, highways, ferries
        'language' => 'en',           // response language
        'traffic_model' => 'best_guess' // best_guess, pessimistic, optimistic
    ]
);

Handling Responses with Caching

// Initialize with custom cache duration (1 hour = 3600 seconds)
$client = new Client( 'your-api-key', true, 3600 );

// Results will be cached
$result = $client->calculate( 'New York, NY', 'Boston, MA' );

// Clear specific cache
$client->clear_cache( 'matrix_New York, NY_Boston, MA' );

// Clear all distance matrix caches
$client->clear_cache();

API Methods

Client Methods

Option Setters

  • set_mode( string $mode ): Set travel mode
  • set_units( string $units ): Set distance units
  • set_avoid(?string $avoid ): Set features to avoid
  • set_language( string $language ): Set response language
  • set_traffic_model(?string $model ): Set traffic model
  • reset_options(): Reset all options to defaults

Core Methods

  • calculate( $origins, $destinations, $options = [] ): Calculate distances
  • clear_cache( $identifier = null ): Clear cached responses

Response Methods

Distance Methods

  • get_distance( $origin_index, $destination_index ): Get distance data for specific pair
  • get_duration( $origin_index, $destination_index ): Get duration data for specific pair
  • get_formatted_distance( $origin_index, $destination_index ): Get formatted distance string
  • get_formatted_duration( $origin_index, $destination_index ): Get formatted duration string
  • get_distance_meters( $origin_index, $destination_index ): Get distance in meters
  • get_duration_seconds( $origin_index, $destination_index ): Get duration in seconds

Data Access Methods

  • get_origins(): Get array of origin addresses
  • get_destinations(): Get array of destination addresses
  • get_all_distances(): Get all calculated distances in array format
  • get_element( $origin_index, $destination_index ): Get specific matrix element
  • get_element_status( $origin_index, $destination_index ): Get status of specific calculation
  • is_complete(): Check if all calculations were successful
  • find_nearest_destination( $origin_index ): Find nearest destination to specific origin

Use Cases

  • Delivery Planning: Calculate delivery routes and times
  • Shipping Costs: Distance-based shipping calculations
  • Service Areas: Define and check service coverage areas
  • Route Optimization: Find optimal routes for multiple destinations
  • Travel Time Estimation: Accurate travel time calculations
  • Fleet Management: Support for vehicle routing and planning
  • Coverage Analysis: Analyze service area coverage
  • Location Planning: Optimize location selection based on distance
  • E-commerce Shipping: Calculate shipping costs based on distance
  • Service Radius: Define service areas with time/distance constraints

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the GPL-2.0-or-later License.

Support

arraypress/google-distance-matrix 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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