Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

mysql php js 经纬度 转换 查询

 Share


Recommended Posts

坐标系介绍

地球坐标 (WGS84)

WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块)
国际标准,从专业GPS 设备中取出的数据的坐标系
国际地图提供商使用的坐标系

火星坐标 (GCJ-02)也叫国测局坐标系

GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用
中国标准,从国行移动设备中定位获取的坐标数据使用这个坐标系
国家规定: 国内出版的各种地图系统(包括电子形式),必须至少采用GCJ-02对地理位置进行首次加密。
腾讯地图用的也是GCJ02坐标

百度坐标 (BD-09)

BD-09:百度坐标偏移标准,Baidu Map使用
百度标准,百度 SDK,百度地图,Geocoding 使用

  1. PHP
/**
 * 中国正常GCJ02坐标---->百度地图BD09坐标
 * 腾讯地图用的也是GCJ02坐标
 * @param double $lat 纬度
 * @param double $lng 经度
 */
function Convert_GCJ02_To_BD09($lat, $lng)
{
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng;
    $y = $lat;
    $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta) + 0.0065;
    $lat = $z * sin($theta) + 0.006;
    return array('lng' => $lng, 'lat' => $lat);
}

/**
 * 百度地图BD09坐标---->中国正常GCJ02坐标
 * 腾讯地图用的也是GCJ02坐标
 * @param double $lat 纬度
 * @param double $lng 经度
 * @return array();
 */
function Convert_BD09_To_GCJ02($lat, $lng)
{
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng - 0.0065;
    $y = $lat - 0.006;
    $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta);
    $lat = $z * sin($theta);
    return array('lng' => $lng, 'lat' => $lat);
}

//用变量替换时注意数据类型
$lng = floatval(112.618056);
$lat = floatval(37.858867);
$locXY = Convert_GCJ02_To_BD09($lng, $lat);
$content = "百度地图坐标系,经度:" . $locXY['lng'] . ";纬度:" . $locXY['lat'];
echo $content;
  1. JS
//将腾讯/高德地图经纬度转换为百度地图经纬度
function qqMapTransBMap(lng, lat) {
    let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    let x = lng;
    let y = lat;
    let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta) + 0.0065;
    let lats = z * Math.sin(theta) + 0.006;

    return {
        lng: lngs,
        lat: lats
    }
}

// 将百度地图经纬度转换为腾讯/高德地图经纬度
function bMapTransQQMap(lng, lat) {
    let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    let x = lng - 0.0065;
    let y = lat - 0.006;
    let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    let lngs = z * Math.cos(theta);
    let lats = z * Math.sin(theta);

    return {
        lng: lngs,
        lat: lats
    }
}

mysql 查询距离

一般地图上显示的坐标顺序为,纬度在前(范围-9090),经度在后(范围-180180)

CREATE TABLE `customer` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `name` varchar(50) NOT NULL COMMENT '名称',
  `lon` double(9,6) NOT NULL COMMENT '经度',
  `lat` double(8,6) NOT NULL COMMENT '纬度',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='商户表';

INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (1, '天津市区', 117.315575, 39.133462);
INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (2, '北京市区', 116.407999, 39.894073);
INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (3, '保定', 115.557124, 38.853490);
INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (4, '石家庄', 114.646458, 38.072369);
INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (5, '昌平区1', 116.367180, 40.009561);
INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (6, '海淀区2', 116.313425, 39.973078);
INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (7, '海淀区1', 116.329236, 39.987231);
INSERT INTO `customer`(`id`, `name`, `lon`, `lat`) VALUES (8, '首开广场', 116.355254, 40.079937);


1168705-20220824154814624-1890000850.png

SELECT
    *,
    ROUND(
        6378.138 * 2 * ASIN(
            SQRT(
                POW(
                    SIN(
                        (
                            40.080335 * PI() / 180 - lat * PI() / 180
                        ) / 2
                    ),
                    2
                ) + COS(40.080335 * PI() / 180) * COS(lat * PI() / 180) * POW(
                    SIN(
                        (
                            116.35511 * PI() / 180 - lon * PI() / 180
                        ) / 2
                    ),
                    2
                )
            )
        ) * 1000
    ) AS juli
FROM
    customer
ORDER BY
    juli ASC

1168705-20220824154840689-579331837.png

Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...