genealabs / laravel-maps
为Laravel提供的简单易用的地图集成。
0.9.0
2023-10-20 12:46 UTC
Requires
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^6.0
- orchestra/testbench-browser-kit: ^6.0
- orchestra/testbench-dusk: ^6.0
- php-coveralls/php-coveralls: *
- phpmd/phpmd: ^2.7
- phpunit/phpunit: ^9.0
- sebastian/phpcpd: ^5.0
- symfony/thanks: ^1.2
Suggests
- geocoder-php/GeocoderLaravel: If you need to geocode addresses and don't already have marker coordinates, use this package. It provides geocoding functionality optimized for Laravel.
This package is auto-updated.
Last update: 2024-09-20 14:51:29 UTC
README
先决条件
- PHP >= 7.3
- Laravel >= 8.0
此包是GeneaLabs/Phpgmaps
的延续。迁移到更合适的包命名空间是为了准备对包进行完整重写,优化其在Laravel中的使用。
PhpGmaps
此仓库旨在维持appitventures/phpgmaps的活跃状态,希望在他们再次提供仓库之前临时填补空缺,或者继续维护并使其与Laravel的未来版本兼容。
安装
将仓库添加到composer.json中的新命名空间下
composer require genealabs/laravel-maps
在你的.env
文件中添加一个包含你的Google Maps API Key的环境变量
GOOGLE_MAPS_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
最后,将以下条目添加到你的\config\services.php
配置文件中
'google' => [ 'maps' => [ 'api-key' => env('GOOGLE_MAPS_API_KEY'), ], ],
示例
以下代码会提示用户获取他们的地理位置访问权限,然后创建一个以他们的经纬度为中心的地图
Route::get('/', function(){
$config = array();
$config['center'] = 'auto';
$config['onboundschanged'] = 'if (!centreGot) {
var mapCentre = map.getCenter();
marker_0.setOptions({
position: new google.maps.LatLng(mapCentre.lat(), mapCentre.lng())
});
}
centreGot = true;';
app('map')->initialize($config);
// set up the marker ready for positioning
// once we know the users location
$marker = array();
app('map')->add_marker($marker);
$map = app('map')->create_map();
echo "<html><head><script type="text/javascript">var centreGot = false;</script>".$map['js']."</head><body>".$map['html']."</body></html>";
});
更多示例
BIOINSTALL有一个非常棒的网站展示了如何使用这个类来做所有的事情。没有必要重新发明轮子,所以这里有。需要注意的是,$this->googlemaps
现在是门面Map::
或者应用变量app('map')
。