Home Backend Development PHP Tutorial 依据经纬度计算距离的公式、百度坐标转换成GPS坐标(PHP版)

依据经纬度计算距离的公式、百度坐标转换成GPS坐标(PHP版)

Jun 13, 2016 pm 01:24 PM
gps return

根据经纬度计算距离的公式、百度坐标转换成GPS坐标(PHP版)

<?php
//百度坐标转换成GPS坐标
$lnglat = '121.437518,31.224665';
function FromBaiduToGpsXY($lnglat){
	// 经度,纬度
	$lnglat = explode(',',$lnglat);
	list($x,$y) = $lnglat;
	$Baidu_Server = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x={$x}&y={$y}";
	$result = @file_get_contents($Baidu_Server);
	$json = json_decode($result);
	if($json->error == 0)
	{
		$bx = base64_decode($json->x);	
		$by = base64_decode($json->y);
		$GPS_x = 2 * $x - $bx;
		$GPS_y = 2 * $y - $by;
		return $GPS_x.','.$GPS_y;//经度,纬度
	}
        else
          return  $lnglat;
}
/**********************************************/
function fn_rad($d)
{
	return $d * pi() / 180.0;
}
// 2点间算法
function P2PDistance($latlng1,$latlng2)
{
	// 纬度1,经度1 ~ 纬度2,经度2
	$latlng1 = explode(',',$latlng1);
	$latlng2 = explode(',',$latlng2);
	list($lat1,$lng1) = $latlng1;
	list($lat2,$lng2) = $latlng2;
	$EARTH_RADIUS = 6378.137;
	$radLat1 = fn_rad($lat1);
	$radLat2 = fn_rad($lat2);
	$a = $radLat1 - $radLat2;
    $b = fn_rad($lng1) - fn_rad($lng2);
	$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
	$s = $s * $EARTH_RADIUS;
	$s = round($s * 10000) / 10000;
	return number_format($s,2);
}
echo '百度坐标: ',$lnglat,'<br><br>','转换后GPS坐标: ',FromBaiduToGpsXY($lnglat),'<br><br>';
echo '转换前距离: ',P2PDistance('31.224286666667,121.420675','31.224665,121.437518'),'<br/>';
echo '转换后距离: ',P2PDistance('31.224286666667,121.420675','31.220157068379,121.42647022694');
?>
Copy after login

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

How to solve Garmin USB device not recognized issue in Windows 11? How to solve Garmin USB device not recognized issue in Windows 11? Apr 22, 2023 am 09:46 AM

GarminGPS itself has the ability to change files, maps, routes and waypoints. However, it's often more efficient to connect the device to your computer and use BaseCamp software. In rare cases, you may find that your computer does not recognize GarminGPS. Don't worry because you're not alone, USB cable errors are usually easy to fix. In the worst case scenario, you will have to buy a new wire for your device. Follow along as we put together a comprehensive list of solutions you can try to fix the error as soon as we see what other products the company makes. Follow us! What products does Garmin make? In addition to GPS devices, the company also makes activity trackers and sports watches that track

How to use GPS toolbox to mark location How to use GPS toolbox to mark location Feb 27, 2024 pm 09:28 PM

GPS toolbox software is a powerful positioning software designed for daily travel and is committed to providing users with the safest and smartest positioning services. Once you download and register the software, it's easy to mark your location accurately. The software has a variety of practical functions, bringing great convenience to users. Whether for personal use or sharing with family, GPS Toolbox makes it easy to mark your location. So how to use the GPS toolbox to mark the location? This tutorial guide will give you a detailed introduction. Users who want to know more can come and continue reading along with this article. How to mark location with gps toolbox? 1. Open the GPS toolbox and find the marked location. 2. Find the calibration. 3. Find the mark note. 4. Click Save

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

Location Services/GPS not working on iPhone [Solved] Location Services/GPS not working on iPhone [Solved] May 18, 2023 pm 08:42 PM

In order to run effectively, some apps require GPS or location services to work properly on iPhone. But many iPhone users reported that location services/GPS suddenly stopped working on their iPhones. There are many reasons why GPS may not work on your iPhone, some of which are listed below. Outdated Apple mobile software location services option is turned off Technical glitch inside iPhone iPhone is located in remote low signal area Incorrect date and time set on iPhone After studying the above points, we have gathered more information and included in the article below A few strong fixes have been compiled for this issue. Preliminary fix for restarting iPhone when iPhone displays certain

60 years in the making: NASA confirms hypothesis on Earth\'s global electric field 60 years in the making: NASA confirms hypothesis on Earth\'s global electric field Aug 31, 2024 pm 09:35 PM

NASA has confirmed the existence of a global electric field around Earth, achieved through a recent rocket launch. This is a big deal, since this is the first direct measurement of a phenomenon which has been long theorized but has never been observe

What is the difference between gps and gnss What is the difference between gps and gnss Feb 02, 2023 pm 03:06 PM

Difference: 1. Different definitions. GPS refers to the Global Positioning System, which is a high-precision radio navigation positioning system based on aerial satellites; while GNSS refers to the Global Navigation Satellite System, which uses the pseudorange, ephemeris, and satellite information of a group of satellites. Observations such as launch time. 2. Different applications. GPS is mainly used for navigation and positioning. It has attracted many users with its high accuracy, all-weather, global coverage, convenience and flexibility; GNSS can provide users with all-weather 3D coordinates at any location on the earth's surface or near-earth space. and speed and time information space-based radio navigation and positioning systems.

What is the difference between gnss and gps What is the difference between gnss and gps Sep 08, 2023 pm 02:38 PM

There is a big difference between gnss and gps, because GNSS (global navigation satellite system) and GPS (global positioning system) are two related but not identical concepts. GNSS is a broad concept that covers various countries and regions around the world. There are multiple satellite navigation systems developed and operated by the United States, while GPS refers solely to the satellite navigation system developed and operated by the United States government.

See all articles