itech-ro/geolocation

Laravel 5.x 的地理位置服务

1.0.4 2018-01-27 23:45 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:26:02 UTC


README

此包提供基于请求IP的地理位置信息,包括国家、城市、纬度和经度。有几个可用的提供者:Extreme IP Lookup、Free GeoIP、Geobytes 和 Geoplugin。

安装

使用composer安装此包

composer require itech-ro/geolocation

编辑 app/config.php 并在提供者数组中添加此行

    'providers' => [
        ...
        Roitech\Geolocation\GeolocationServiceProvider::class,
        ...
    ];

创建一个配置文件 config/geolocation.php 并包含以下内容

<?php

return [

    'provider' => 'geoplugin',

];

如何使用它

一旦在 config/geolocation.php 中设置了首选提供者(可能的值:geobytes、freegeoip、extremeiplookup、geoplugin),就可以通过 Geolocation 门面访问地理位置详细信息

$coordinates = Geolocation::getCoordinates(); //returns an array [ 'lat' => LATITUDE, 'long' => LONGITUDE ]
$city = Geolocation::getCity();
$country = Geolocation::getCountry();