martingold/yii2-google-maps-library

为 Yii2 的 Google Maps API 库。

安装次数: 1,854

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 78

类型:yii-extension

v2.0.0 2023-11-30 13:49 UTC

This package is auto-updated.

Last update: 2024-09-22 15:28:35 UTC


README

为 Yii2 的 Google Maps API 库

Latest Stable Version Total Downloads Latest Unstable Version License

简介

尽管我们已经创建了一个扩展来显示不受谷歌政策限制的地图,并且与 LeafLetJs 库兼容,但我们仍然收到请求,希望更新 EGMap 扩展为 Yii1。因此,我们认为我们应该更新这个库,使其与 Yii2 兼容。这就是创建这个扩展的原因。

然而,值得注意的是,我们还没有时间(目前)写任何好的文档。我们想早点发布它,以防有使用 Yii2 的开发者缺少 EGMap 库,并希望向我们提供测试和错误发现。

github 仓库将持续更新,并为其使用编写良好的文档。所以,请不要急躁。如果您愿意,任何帮助都将非常感激。

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

php composer.phar require "2amigos/yii2-google-maps-library" "*"

或者

"2amigos/yii2-google-maps-library" : "*"

将其添加到应用程序 composer.json 文件的 require 部分。

用法

尽管将提供大量使用示例,但以下示例将为您提供对其使用的初步了解:

服务

use dosamigos\google\maps\LatLng;
use dosamigos\google\maps\services\DirectionsWayPoint;
use dosamigos\google\maps\services\TravelMode;
use dosamigos\google\maps\overlays\PolylineOptions;
use dosamigos\google\maps\services\DirectionsRenderer;
use dosamigos\google\maps\services\DirectionsService;
use dosamigos\google\maps\overlays\InfoWindow;
use dosamigos\google\maps\overlays\Marker;
use dosamigos\google\maps\Map;
use dosamigos\google\maps\services\DirectionsRequest;
use dosamigos\google\maps\overlays\Polygon;
use dosamigos\google\maps\layers\BicyclingLayer;

$coord = new LatLng(['lat' => 39.720089311812094, 'lng' => 2.91165944519042]);
$map = new Map([
    'center' => $coord,
    'zoom' => 14,
]);

// lets use the directions renderer
$home = new LatLng(['lat' => 39.720991014764536, 'lng' => 2.911801719665541]);
$school = new LatLng(['lat' => 39.719456079114956, 'lng' => 2.8979293346405166]);
$santo_domingo = new LatLng(['lat' => 39.72118906848983, 'lng' => 2.907628202438368]);

// setup just one waypoint (Google allows a max of 8)
$waypoints = [
    new DirectionsWayPoint(['location' => $santo_domingo])
];

$directionsRequest = new DirectionsRequest([
    'origin' => $home,
    'destination' => $school,
    'waypoints' => $waypoints,
    'travelMode' => TravelMode::DRIVING
]);

// Lets configure the polyline that renders the direction
$polylineOptions = new PolylineOptions([
    'strokeColor' => '#FFAA00',
    'draggable' => true
]);

// Now the renderer
$directionsRenderer = new DirectionsRenderer([
    'map' => $map->getName(),
    'polylineOptions' => $polylineOptions
]);

// Finally the directions service
$directionsService = new DirectionsService([
    'directionsRenderer' => $directionsRenderer,
    'directionsRequest' => $directionsRequest
]);

// Thats it, append the resulting script to the map
$map->appendScript($directionsService->getJs());

// Lets add a marker now
$marker = new Marker([
    'position' => $coord,
    'title' => 'My Home Town',
]);

// Provide a shared InfoWindow to the marker
$marker->attachInfoWindow(
    new InfoWindow([
        'content' => '<p>This is my super cool content</p>'
    ])
);

// Add marker to the map
$map->addOverlay($marker);

// Now lets write a polygon
$coords = [
    new LatLng(['lat' => 25.774252, 'lng' => -80.190262]),
    new LatLng(['lat' => 18.466465, 'lng' => -66.118292]),
    new LatLng(['lat' => 32.321384, 'lng' => -64.75737]),
    new LatLng(['lat' => 25.774252, 'lng' => -80.190262])
];

$polygon = new Polygon([
    'paths' => $coords
]);

// Add a shared info window
$polygon->attachInfoWindow(new InfoWindow([
        'content' => '<p>This is my super cool Polygon</p>'
    ]));

// Add it now to the map
$map->addOverlay($polygon);


// Lets show the BicyclingLayer :)
$bikeLayer = new BicyclingLayer(['map' => $map->getName()]);

// Append its resulting script
$map->appendScript($bikeLayer->getJs());

// Display the map -finally :)
echo $map->display();

客户端

use dosamigos\google\maps\services\DirectionsClient;

$direction = new DirectionsClient([
    'params' => [
        'language' => Yii::$app->language,
        'origin' => 'street from',
        'destination' => 'street to'
    ]
]);

$data = $direction->lookup(); //get data from google.maps API

此扩展还具有插件架构,允许我们增强它,因此预计未来也将开发插件。

配置

要配置 Google 地图密钥或其他选项(如语言、版本、库),请使用 资产包定制 功能。

'components' => [
    'assetManager' => [
        'bundles' => [
            'dosamigos\google\maps\MapAsset' => [
                'options' => [
                    'key' => 'this_is_my_key',
                    'language' => 'id',
                    'version' => '3.1.18'
                ]
            ]
        ]
    ],
],

获取密钥,请访问 https://code.google.com/apis/console/

使用代码嗅探器

运行 composer install -o 后,php-cs-fixer 工具将被添加到您的 vendor 文件中。要修复 PHP 文件的格式,只需执行以下操作:

 ./vendor/bin/php-cs-fixer --config=.php_cs.dist fix

资源

2amigOS!
beyond software
www.2amigos.us