Home  >  Article  >  Backend Development  >  How to convert Baidu longitude and latitude to Tencent longitude and latitude using PHP

How to convert Baidu longitude and latitude to Tencent longitude and latitude using PHP

PHPz
PHPzOriginal
2023-04-19 09:21:561107browse

During the development process, we usually need to process some geographical location information, and the longitude and latitude formats of different platforms are different, which requires conversion. This article will introduce how to convert Baidu longitude and latitude into Tencent longitude and latitude, and use PHP code to implement it.

1. The difference between Baidu’s longitude and latitude and Tencent’s longitude and latitude

Longitude and latitude are symbols of the position on the earth’s surface. They are expressed in different ways in different positioning systems. There are currently three mainstream positioning systems: : WGS84, GCJ02 and BD09. Among them, WGS84 is the coordinate system used by the GPS positioning system, GCJ02 is the coordinate system of the geographic information system formulated by the China National Bureau of Surveying and Mapping, and is also the coordinate system that must be used by major domestic map software, while Baidu Maps uses BD09.

When using different positioning systems, the performance of longitude and latitude will also be different. For example, the Baidu longitude and latitude and Tencent longitude and latitude of a location are as follows:

Baidu longitude and latitude: 116.404,39.915
Tencent latitude and longitude: 116.397428,39.908697

2. Conversion method

Since Baidu and Tencent use different positioning systems, a certain algorithm is required to convert between longitude and latitude.

  1. Convert Baidu coordinate system to Earth coordinate system (WGS84)

First you need to convert Baidu coordinate system to Earth coordinate system (WGS84), which can be provided by Baidu API implementation, the code is as follows:

<?php
function bd09_to_wgs84($bd_lon,$bd_lat){
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $bd_lon - 0.0065;
    $y = $bd_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(&#39;lng&#39;=>$lng, 'lat'=>$lat);
}
?>
  1. Convert the Earth coordinate system (WGS84) to the Mars coordinate system (GCJ02)

Use the function in the first step to get the earth After obtaining the result in the coordinate system (WGS84), it needs to be converted to the Mars coordinate system. This can be achieved by the following code:

<?php
function wgs84_to_gcj02($lng, $lat) {
    $a = 6378245.0;
    $ee = 0.00669342162296594323;
    $dLat = $this->transformLat($lng - 105.0, $lat - 35.0);
    $dLng = $this->transformLng($lng - 105.0, $lat - 35.0);
    $radLat = $lat / 180.0 * pi();
    $magic = sin($radLat);
    $magic = 1 - $ee * $magic * $magic;
    $sqrtMagic = sqrt($magic);
    $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * pi());
    $dLng = ($dLng * 180.0) / ($a / $sqrtMagic * cos($radLat) * pi());
    $mgLat = $lat + $dLat;
    $mgLng = $lng + $dLng;
    return array('lng'=>$mgLng, 'lat'=>$mgLat);
}
function transformLat($lng, $lat) {
    $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
    $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0;
    $ret += (20.0 * sin($lat * pi()) + 40.0 * sin($lat / 3.0 * pi())) * 2.0 / 3.0;
    $ret += (160.0 * sin($lat / 12.0 * pi()) + 320 * sin($lat * pi() / 30.0)) * 2.0 / 3.0;
    return $ret;
}
function transformLng($lng, $lat) {
    $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
    $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0;
    $ret += (20.0 * sin($lng * pi()) + 40.0 * sin($lng / 3.0 * pi())) * 2.0 / 3.0;
    $ret += (150.0 * sin($lng / 12.0 * pi()) + 300.0 * sin($lng / 30.0 * pi())) * 2.0 / 3.0;
    return $ret;
}
?>
  1. Convert the Mars coordinate system (GCJ02) to the Tencent coordinate system

The last step is to convert the Mars coordinate system (GCJ02) to Tencent coordinate system:

<?php
function gcj02_to_tx($lng, $lat) {
    $x = $lng;
    $y = $lat;
    $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * pi());
    $theta = atan2($y, $x) + 0.000003 * cos($x * pi());
    $lng = $z * cos($theta) + 0.0065;
    $lat = $z * sin($theta) + 0.006;
    return array(&#39;lng&#39;=>$lng, 'lat'=>$lat);
}
?>

3. Complete code implementation

Combine the above three steps to get the complete code for converting Baidu longitude and latitude into Tencent longitude and latitude in PHP as follows:

<?php
function bd09_to_wgs84($bd_lon,$bd_lat){
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $bd_lon - 0.0065;
    $y = $bd_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(&#39;lng&#39;=>$lng, 'lat'=>$lat);
}

function wgs84_to_gcj02($lng, $lat) {
    $a = 6378245.0;
    $ee = 0.00669342162296594323;
    $dLat = $this->transformLat($lng - 105.0, $lat - 35.0);
    $dLng = $this->transformLng($lng - 105.0, $lat - 35.0);
    $radLat = $lat / 180.0 * pi();
    $magic = sin($radLat);
    $magic = 1 - $ee * $magic * $magic;
    $sqrtMagic = sqrt($magic);
    $dLat = ($dLat * 180.0) / (($a * (1 - $ee)) / ($magic * $sqrtMagic) * pi());
    $dLng = ($dLng * 180.0) / ($a / $sqrtMagic * cos($radLat) * pi());
    $mgLat = $lat + $dLat;
    $mgLng = $lng + $dLng;
    return array('lng'=>$mgLng, 'lat'=>$mgLat);
}
function transformLat($lng, $lat) {
    $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
    $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0;
    $ret += (20.0 * sin($lat * pi()) + 40.0 * sin($lat / 3.0 * pi())) * 2.0 / 3.0;
    $ret += (160.0 * sin($lat / 12.0 * pi()) + 320 * sin($lat * pi() / 30.0)) * 2.0 / 3.0;
    return $ret;
}
function transformLng($lng, $lat) {
    $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
    $ret += (20.0 * sin(6.0 * $lng * pi()) + 20.0 * sin(2.0 * $lng * pi())) * 2.0 / 3.0;
    $ret += (20.0 * sin($lng * pi()) + 40.0 * sin($lng / 3.0 * pi())) * 2.0 / 3.0;
    $ret += (150.0 * sin($lng / 12.0 * pi()) + 300.0 * sin($lng / 30.0 * pi())) * 2.0 / 3.0;
    return $ret;
}

function gcj02_to_tx($lng, $lat) {
    $x = $lng;
    $y = $lat;
    $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * pi());
    $theta = atan2($y, $x) + 0.000003 * cos($x * pi());
    $lng = $z * cos($theta) + 0.0065;
    $lat = $z * sin($theta) + 0.006;
    return array('lng'=>$lng, 'lat'=>$lat);
}

function bd09_to_tx($bd_lon, $bd_lat) {
    $point_wgs84 = $this->bd09_to_wgs84($bd_lon, $bd_lat);
    $point_gcj02 = $this->wgs84_to_gcj02($point_wgs84['lng'], $point_wgs84['lat']);
    $point_tx = $this->gcj02_to_tx($point_gcj02['lng'], $point_gcj02['lat']);
    return $point_tx;
}
?>

Save the above code in a PHP file and you can use it.

4. Summary

Through the introduction of this article, we have learned the difference between Baidu longitude and latitude and Tencent longitude and latitude, and mastered the method of using PHP code to convert Baidu longitude and latitude into Tencent longitude and latitude. In actual projects, this conversion method can provide us with more convenient and accurate map information processing functions.

The above is the detailed content of How to convert Baidu longitude and latitude to Tencent longitude and latitude using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn