Home Backend Development PHP Problem 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

Apr 19, 2023 am 09:21 AM

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);
}
?>
Copy after login
  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;
}
?>
Copy after login
  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);
}
?>
Copy after login

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;
}
?>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

How do you retrieve data from a database using PHP? How do you retrieve data from a database using PHP? Mar 20, 2025 pm 04:57 PM

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

PHP API Rate Limiting: Implementation strategies. PHP API Rate Limiting: Implementation strategies. Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

See all articles