Home  >  Article  >  Backend Development  >  How to use Amap API in php to get the distance between two points

How to use Amap API in php to get the distance between two points

WBOY
WBOYOriginal
2023-07-30 08:53:281529browse

How to use the Amap API in php to get the distance between two points

Introduction:
With the rapid development of the mobile Internet, geographical location information has become one of the important application scenarios. Using the map API, you can easily obtain the distance between two locations, which is very important for applications such as location navigation and travel planning. This article will introduce how to use the Amap API in PHP to obtain the distance between two points, helping readers make better use of geographical information.

Step 1: Apply for an Amap developer account and API Key
First, you need to register a developer account on the Amap open platform and apply for an API Key. API Key is a certificate for using the AMAP API. Each developer account can apply for a unique API Key.

Step 2: Introduce the Amap API
At the beginning of the PHP file, use the following code to introduce the JavaScript API of the Amap:

<script src="http://webapi.amap.com/maps?v=1.4.15&key=YOUR_API_KEY"></script>

Among them, YOUR_API_KEY needs to be replaced with the one you applied for to the API Key.

Step 3: Use the Amap API to get the distance
The following is a simple sample code that shows how to use the Amap API to get the distance between two places:

<?php
// 定义起点坐标
$start_lng = 116.397428;
$start_lat = 39.90923;

// 定义终点坐标
$end_lng = 116.413554;
$end_lat = 39.912864;

// 构造请求的URL
$url = "http://restapi.amap.com/v3/distance?origin=".$start_lng.",".$start_lat."&destination=".$end_lng.",".$end_lat."&key=YOUR_API_KEY";

// 发送请求并获取结果
$response = file_get_contents($url);

// 解析返回的JSON数据
$data = json_decode($response, true);

// 解析得到距离
if($data['status'] == 1){
    $distance = $data['results'][0]['distance'];
    echo "两地之间的距离为:" . $distance . "米";
}else{
    echo "请求失败,请检查参数是否正确";
}
?>

In the code, first define the latitude and longitude coordinates of the starting point and end point. Then, construct the requested URL, which includes the starting point, end point coordinates and API Key. Use the file_get_contents() function to send an HTTP request and obtain the returned JSON data. Finally, parse the JSON data, obtain the distance between the two places and output it.

Summary:
Through the above steps, we can easily use the Amap API in PHP to obtain the distance between two locations. This is very useful for rational planning of travel routes, navigation and other applications. I hope this article can help readers make better use of geographic information and achieve more interesting application scenarios.

The above is the detailed content of How to use Amap API in php to get the distance between two points. 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