bodunde/geocoder

一个Laravel包,帮助您使用Google Maps API进行地址编码和反向地理编码坐标。它还可以帮助使用Haversine公式和位置坐标计算两个位置之间的距离

v1.2.1 2016-10-09 18:19 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:31 UTC


README

一个Laravel包,通过Google Maps API接口帮助将普通地址转换为经纬度坐标,反之亦然。它还可以根据两个位置的坐标计算它们之间的距离(千米或英里)。

安装

  • 将包包含在您的 composer.json 文件中,并运行 composer update,或者运行 composer require bodunde/geocoder,或者您可以像老式一样克隆存储库并将其包含在您的应用程序中。
  • 导航到您的Laravel应用程序中的 config 目录,并找到 app.php 文件
  • 将此行添加到服务提供者 Bodunde\GoogleGeocoder\GeocoderServiceProvider::class
  • 运行 php artisan vendor:publish 以发布其配置文件
  • 在配置目录中位于 geocoder.php 文件中的 google_api_key 中包含您的API密钥!Voila!!! 您已完成

用法

<?php
namespace Your\Namespace;

use Bodunde\GoogleGeocoder\Geocoder;
...
...
// using dependency injection
public function index(Geocoder $geocoder)
{
  // get coordinates
  $coordinates = $geocoder->getCoordinates('55 Moleye Street, Yaba');

  // get address via reverse geocoding
  $addressCoordinates = [
    "lat" => 6.5349646,
    "lng" => 3.3892894
  ];
  $address = $geocoder->getAddress($addressCoordinates);

  // get distance between two locations
  $location1 = [
    "lat" => 6.5349646,
    "lng" => 3.3892894
  ];

  $location2 = [
    "lat" => 6.601838,
    "lng" => 3.3514863
  ];
  $distance = $geocoder->getDistanceBetween($location1, $location2);

  /** geocoder can also be instantiated normally without DI */
  //e.g $geocoder = new Geocoder;
}

测试

在包根目录下运行 phpunit