php gps to gcj-02 method: 1. Create a php sample file; 2. Use the "public static function wgs84ToGcj02 (float $lng, float $lat):array {...}" method to convert Just convert WGS84 to GCJ02.

The operating environment of this tutorial: windows10 system, PHP8.1 version, DELL G3 computer
How to convert php gps to gcj-02 ?
php Longitude and latitude coordinate conversion WGS84, Mars coordinates (GCJ-02), Baidu coordinates (BD-09)
The project has a gps reporting function. Due to front-end plug-in problems, a large number of gps There was a huge offset when the positioning data was converted into Baidu coordinates (BD-09), so the backend needed to be converted into longitude and latitude coordinates. I saw a technical post about Java and used it to make modifications
Ps: Coordinates There is a slight deviation in the conversion, but it is within the acceptable range
Baidu longitude and latitude correction api: http://api.map.baidu.com/ag/coord/convert
php code:
<?php
namespace App\Tool;
/**
* Class GpsUtils
* GCJ-02 -- 由国测局制定的GCJ-02 标准,高德地图,腾讯地图,谷歌地图中国大陆板块均采用此标准
* @package App\Tool
*/
class GpsUtils {
const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
// π
const pi = 3.1415926535897932384626;
// 长半轴
const a = 6378245.0;
// 扁率
const ee = 0.00669342162296594323;
/**
* 百度坐标系(BD-09)转WGS坐标
*
* @param float $lng 百度坐标纬度
* @param float $lat 百度坐标经度
* @return array WGS84坐标数组
*/
public static function bd09ToWgs84 (float $lng, float $lat): array {
$gcj = self::bd09ToGcj02($lng, $lat);
return self::gcj02ToWgs84($gcj[0], $gcj[1]);
}
/**
* WGS坐标转百度坐标系(BD-09)
*
* @param float $lng WGS84坐标系的经度
* @param float $lat WGS84坐标系的纬度
* @return array 百度坐标数组
*/
public static function wgs84ToBd09 (float $lng, float $lat): array {
$gcj = self::wgs84ToGcj02($lng, $lat);
return self::gcj02ToBd09($gcj[0], $gcj[1]);
}
/**
* 火星坐标系(GCJ-02)转百度坐标系(BD-09)
*
* @param float $lng 火星坐标经度
* @param float $lat 火星坐标纬度
* @return array 百度坐标数组
* @see 谷歌、高德——>百度
*/
public static function gcj02ToBd09 (float $lng, float $lat): array {
$z = sqrt($lng * $lng + $lat * $lat) + 0.00002 * sin($lat * self::x_pi);
$theta = atan2($lat, $lng) + 0.000003 * cos($lng * self::x_pi);
$bd_lng = $z * cos($theta) + 0.0065;
$bd_lat = $z * sin($theta) + 0.006;
return [$bd_lng, $bd_lat];
}
/**
* 百度坐标系(BD-09)转火星坐标系(GCJ-02)
*
* @param float $bd_lon 百度坐标纬度
* @param float $bd_lat 百度坐标经度
* @return array
* @see 百度——>谷歌、高德
*/
public static function bd09ToGcj02 (float $bd_lon, float $bd_lat): array {
$x = $bd_lon - 0.0065;
$y = $bd_lat - 0.006;
$z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * self::x_pi);
$theta = atan2($y, $x) - 0.000003 * cos($x * self::x_pi);
$gg_lng = $z * cos($theta);
$gg_lat = $z * sin($theta);
return [$gg_lng, $gg_lat];
}
/**
* WGS84转GCJ02(火星坐标系)
*
* @param float $lng WGS84坐标系的经度
* @param float $lat WGS84坐标系的纬度
* @return array 火星坐标数组
*/
public static function wgs84ToGcj02 (float $lng, float $lat): array {
$d_lat = self::transformlat($lng - 105.0, $lat - 35.0);
$d_lng = self::transformlng($lng - 105.0, $lat - 35.0);
$rad_lat = $lat / 180.0 * self::pi;
$magic = sin($rad_lat);
$magic = 1 - self::ee * $magic * $magic;
$sqrt_magic = sqrt($magic);
$d_lat = ($d_lat * 180.0) / ((self::a * (1 - self::ee)) / ($magic * $sqrt_magic) * self::pi);
$d_lng = ($d_lng * 180.0) / (self::a / $sqrt_magic * cos($rad_lat) * self::pi);
$mg_lat = $lat + $d_lat;
$mg_lng = $lng + $d_lng;
return [$mg_lng, $mg_lat];
}
/**
* GCJ02(火星坐标系)转GPS84
* @param float $lng 火星坐标系的经度
* @param float $lat 火星坐标系纬度
* @return array WGS84坐标数组
*/
public static function gcj02ToWgs84 (float $lng, float $lat): array {
$d_lat = self::transformlat($lng - 105.0, $lat - 35.0);
$d_lng = self::transformlng($lng - 105.0, $lat - 35.0);
$rad_lat = $lat / 180.0 * self::pi;
$magic = sin($rad_lat);
$magic = 1 - self::ee * $magic * $magic;
$sqrt_magic = sqrt($magic);
$d_lat = ($d_lat * 180.0) / ((self::a * (1 - self::ee)) / ($magic * $sqrt_magic) * self::pi);
$d_lng = ($d_lng * 180.0) / (self::a / $sqrt_magic * cos($rad_lat) * self::pi);
$mg_lat = $lat + $d_lat;
$mg_lng = $lng + $d_lng;
return [$lng * 2 - $mg_lng, $lat * 2 - $mg_lat];
}
/**
* 纬度转换
* @param float $lng
* @param float $lat
* @return float|int
*/
public static function transFormLat (float $lng, float $lat): float {
$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 * self::pi) + 20.0 * sin(2.0 * $lng * self::pi)) * 2.0 / 3.0;
$ret += (20.0 * sin($lat * self::pi) + 40.0 * sin($lat / 3.0 * self::pi)) * 2.0 / 3.0;
$ret += (160.0 * sin($lat / 12.0 * self::pi) + 320 * sin($lat * self::pi / 30.0)) * 2.0 / 3.0;
return $ret;
}
/**
* 经度转换
* @param float $lng
* @param float $lat
* @return float
*/
public static function transFormLng (float $lng, float $lat): float {
$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 * self::pi) + 20.0 * sin(2.0 * $lng * self::pi)) * 2.0 / 3.0;
$ret += (20.0 * sin($lng * self::pi) + 40.0 * sin($lng / 3.0 * self::pi)) * 2.0 / 3.0;
$ret += (150.0 * sin($lng / 12.0 * self::pi) + 300.0 * sin($lng / 30.0 * self::pi)) * 2.0 / 3.0;
return $ret;
}
}Related expansion:
GCJ-02 is the coordinate system of the geographic information system developed by the China National Bureau of Surveying and Mapping (G represents Guojia country, C represents Cehui surveying and mapping, and J represents Ju).
It is an encryption algorithm for latitude and longitude data, that is, adding random deviations.
Various map systems published in China (including electronic forms) must use at least GCJ-02 to encrypt the geographical location for the first time.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert php gps to gcj-02. For more information, please follow other related articles on the PHP Chinese website!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe 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.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe 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 Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe 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 XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.






