kematjaya / leaflet-bundle
基于 leaflet.js 提供地图,用于获取经纬度
1.0.4
2022-12-20 04:22 UTC
Requires
- symfony/form: ^5.0
- symfony/framework-bundle: ^5.0
- symfony/twig-bundle: ^5.0
- symfony/yaml: ^5.0
README
基于 leaflet js 的 symfony 扩展包,用于通过地图选择位置
- 安装
composer kematjaya/leaflet-bundle
- 添加到 config/bundles.php
// add to config/bundles.php
...
Kematjaya\LeafletBundle\LeafletBundle::class => ['all' => true]
...
- 添加到表单
// src/Form/AddressType.php
...
use Kematjaya\LeafletBundle\Type\LeafletMapType
...
$builder
->add('location', LeafletMapType::class, [
'label' => 'location',
"map_height" => "350px", // default 250px
"map_width" => "100%", // default 100%
]);
...
- 创建配置文件 config/packages/leaflet.yaml
leaflet:
map_box:
api_token: '%env(resolve:LEAFLET_MAPBOX_TOKEN)%'
map:
lock_map: true # lock map at center point, default true
lock_coordinates:
southwest: "-7.777488, 114.776975" # point lock
northeast: "-7.403438, 110.834149" # point lock
min_zoom: 8 # minimal zoom available, default 8
max_zoom: 20 # maximal zoom available, default 20
zoom_value: 11 # default zoom when map loaded
on_click_zoom: 14 # zoom when map clicked, default 14
center_point: '%env(resolve:LEAFLET_MAP_CENTER_POINT)%'
zoom_point: '%env(resolve:LEAFLET_MAP_CENTER_POINT)%'
并添加到 .env 文件
LEAFLET_MAPBOX_TOKEN=your.mapbox.token
LEAFLET_MAP_CENTER_POINT=longlat point ## example: '-7.293421341699741, 112.73709354459358'
- 使用距离计算器
...
use Kematjaya\LeafletBundle\Calculator\Point;
use Kematjaya\LeafletBundle\Calculator\DistanceCalculatorInterface;
...
public function test(DistanceCalculatorInterface $distanceCalculator)
{
$from = new Point(-7.345728218434821, 112.76383132697055);
$to = new Point(-7.2491223553386375, 112.79650342712807);
$distance = $distanceCalculator->getDistance($from, $to); // in KM
}