PHP code example of jundayw / location-based-services
1. Go to this page and download the library: Download jundayw/location-based-services library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jundayw / location-based-services example snippets
// 标准坐标格式
$points = [
[116.4028930664, 39.9034155951],
[-114.3347167969, -30.5433389542],
];
// 其他支持的坐标格式
$points = [
["东经98度2分43秒", "南纬24度45分16.1秒"],
["98°2′43.8″E", "24°45′16.1″S"],
["E98°2′43.8″", "S24°45′16.1″"],
["98°59′59.9″E", "24°59′59.9″N"],
["98°59′59.9″W", "24°59′59.9″S"],
["98.5°E", "24.5°S"],
["98.5E", "24.5S"],
[98.5, -24.5],
];
$position = new Position();
$position->setDecimals(2);
// 标准坐标计算距离
$distance = $position->getDistance(
new Point(116.4028930664, 39.9034155951),
new Point(114.3347167969, 30.5433389542)
);
// 结果:1058703.49
// 其他格式标准计算距离
$distance = $position->getDistance(
new Converter("东经98度58分45.8秒", "南纬24度45分16.1秒"),
new Converter("116°23′28.4″E", "39°54′26.2″N")
);
// 结果:7421559.99
$position = new Position(new LengthUnit("KM"));
$position->setDecimals(2);
// 标准坐标计算距离
$distance = $position->getDistanceUnit(
new Point(116.4028930664, 39.9034155951),
new Point(114.3347167969, 30.5433389542)
);
// 结果:1058.7KM
// 其他格式标准计算距离
$distance = $position->getDistanceUnit(
new Converter("东经98度58分45.8秒", "南纬24度45分16.1秒"),
new Converter("116°23′28.4″E", "39°54′26.2″N")
);
// 结果:7421.56KM
$point = new Point(98.979388888889, -24.754472222222);
$point = new Converter("东经98度58分45.8秒", "南纬24度45分16.1秒");
var_dump($point->parse());
array (
'longitude' => array (
'DDD' => 98.979388888889,
'ddd' => 98.979388888889,
'D' => 98,
'd' => 98,
'm' => 58,
's' => 45.8,
'L' => 'E',
),
'latitude' => array (
'DDD' => -24.754472222222,
'ddd' => 24.754472222222,
'D' => -24,
'd' => 24,
'm' => 45,
's' => 16.1,
'L' => 'S',
),
)
$point = new Point(98.979388888889, -24.754472222222);
$point = new Converter("东经98度58分45.8秒", "南纬24度45分16.1秒");
var_dump($point->parse(2, true));
array (
'longitude' => array (
'DDD' => 98.979388888889,
'ddd' => 98.979388888889,
'D' => 98,
'd' => 98,
'm' => 58,
's' => 45.8,
'L' => '东经',
),
'latitude' => array (
'DDD' => -24.754472222222,
'ddd' => 24.754472222222,
'D' => -24,
'd' => 24,
'm' => 45,
's' => 16.1,
'L' => '南纬',
),
)
$point = new Point(98.979388888889, -24.754472222222);
$point = new Converter("东经98度58分45.8秒", "南纬24度45分16.1秒");
var_export($point->format("Ld度m分s秒", true));
array (
'longitude' => '东经98度58分45.8秒',
'latitude' => '南纬24度45分16.1秒',
)
$point = new Point(98.979388888889, -24.754472222222);
$point = new Converter("东经98度58分45.8秒", "南纬24度45分16.1秒");
var_export($point->format());
array (
'longitude' => '98°58′45.8″E',
'latitude' => '24°45′16.1″S',
)
$points = [
[116.4028930664, 39.9034155951],
[-114.3347167969, -30.5433389542],
];
foreach ($points as $point) {
$point = new Point($point[0], $point[1]);
}
// WGS84(GPS坐标系)/GCJ02(火星坐标系)/BD09(百度坐标系)之间可以互相任意转换
$translate = new Translate(WGS84::class, GCJ02::class);// GPS坐标系转火星坐标系
$translate = new Translate(WGS84::class, BD09::class);// GPS坐标系转百度坐标系
$translate = new Translate(BD09::class, GCJ02::class);// 百度坐标系转火星坐标系
$translate = new Translate(BD09::class, WGS84::class);// 百度坐标系转GPS坐标系
$point = $translate->translate($point);// 转换后坐标
$points = [
["东经98度2分43秒", "南纬24度45分16.1秒"],
["98°2′43.8″E", "24°45′16.1″S"],
["E98°2′43.8″", "S24°45′16.1″"],
["98°59′59.9″E", "24°59′59.9″N"],
["98°59′59.9″W", "24°59′59.9″S"],
["98.5°E", "24.5°S"],
["98.5E", "24.5S"],
["98.5", "-24.5"],
[116.4028930664, 39.9034155951],
[-114.3347167969, -30.5433389542],
];
foreach ($points as $point) {
$converter = new Converter($point[0], $point[1]);
}
// WGS84(GPS坐标系)/GCJ02(火星坐标系)/BD09(百度坐标系)之间可以互相任意转换
$translate = new Translate(WGS84::class, GCJ02::class);// GPS坐标系转火星坐标系
$translate = new Translate(WGS84::class, BD09::class);// GPS坐标系转百度坐标系
$translate = new Translate(BD09::class, GCJ02::class);// 百度坐标系转火星坐标系
$translate = new Translate(BD09::class, WGS84::class);// 百度坐标系转GPS坐标系
$point = $translate->translate($converter);// 转换后坐标
$start = new Point(106.0, 39.0);// GPS 坐标
$end = new Point(107.0, 40.0);// GPS 坐标
$position = new Position();
$gps = $position->getDistance($start, $end);
$translate = new Translate(WGS84::class, BD09::class);
$bd09 = $position->getDistance($translate->translate($start), $translate->translate($end));
[
"gps" => 140604.6, // GPS 坐标系距离
"bd09" => 140673.57, // 百度坐标系距离
];
$start = new Point(1.0, 39.0);// GPS 坐标
$end = new Point(1.0, 40.0);// GPS 坐标
$position = new Position();
$gps = $position->getDistance($start, $end);
$translate = new Translate(WGS84::class, BD09::class);
$bd09 = $position->getDistance($translate->translate($start), $translate->translate($end));
[
"gps" => 111319.49, // GPS 坐标系距离
"bd09" => 111319.49, // 百度坐标系距离
];
├─Position.php // 通过坐标计算距离
├─Circles
│ Circle.php // 接口
│ Earth.php // 地球相关参数
├─Point
│ AbstractPoint.php // 将标准坐标格式转换为不同坐标格式
│ Point.php // 标准坐标格式
├─Converters
│ Converter.php // 将不同坐标格式转换为标准坐标格式
└─Units
LengthUnit.php // 长度单位换算
Units.php // 接口
├─Translate.php // 坐标系转换,如:GPS、火星坐标(Google/高德)、百度坐标系
└─Geographies
BD09.php // 百度坐标系编码、解码
GCJ02.php // 火星坐标系编码、解码
Geography.php // 坐标系基础配置
WGS84.php // GPS坐标系编码、解码