Home  >  Article  >  Backend Development  >  How to use Amap API in php to obtain detailed information of surrounding POIs

How to use Amap API in php to obtain detailed information of surrounding POIs

王林
王林Original
2023-07-30 09:01:301547browse

How to use Amap API in php to obtain detailed information of surrounding POIs

Introduction:
With the rapid development of modern society, navigation software and map applications have become indispensable in our daily lives a part of. As the leading map service provider in China, Amap's open API interface provides developers with a wealth of geographical information and functions. This article will introduce how to use the Amap API in PHP to obtain detailed information about surrounding POIs (Points of Interest), and provide corresponding code examples.

1. Preparation:

Before starting, we need to do the following preparations:

  1. Obtain the Amap developer account and API key: Register a developer account on the Amap open platform and obtain the corresponding API key. The API key is the authentication parameter that we must carry when requesting the Amap API.
  2. Set up the php environment: Make sure php is installed on your server and the environment is configured correctly.

2. Code implementation:

The following is a sample code that uses PHP to call the Amap API to obtain surrounding POI detailed information:

<?php
// 定义高德地图API密钥
$apiKey = "Your API Key";

// 定义请求接口的URL
$url = "https://restapi.amap.com/v3/place/around?";

// 定义请求参数
$location = "116.397428,39.90923"; // 中心点坐标,例如:经度,纬度
$radius = 1000; // 查询半径,单位:米
$keywords = "美食"; // 关键词,例如:餐厅、银行等
$types = ""; // POI类型,例如:餐饮服务、银行等
$offset = 20; // 单页显示结果数,默认为20,最大为50
$page = 1; // 当前页数,默认为1

// 拼接请求URL
$requestUrl = $url . "key=" . $apiKey . "&location=" . $location . "&radius=" . $radius . "&keywords=" . $keywords . "&types=" . $types . "&offset=" . $offset . "&page=" . $page;

// 发送HTTP请求获取数据
$response = file_get_contents($requestUrl);
$data = json_decode($response, true);

// 输出结果
if ($data['status'] == '1') {
    $pois = $data['pois'];
    foreach ($pois as $poi) {
        echo "名称:" . $poi['name'] . "<br>";
        echo "地址:" . $poi['address'] . "<br>";
        echo "电话:" . $poi['tel'] . "<br>";
        echo "经纬度:" . $poi['location'] . "<br>";
        echo "------------------------<br>";
    }
} else {
    echo "请求失败!" . "<br>";
    echo "错误信息:" . $data['info'] . "<br>";
}

?>

The above code is obtained After requesting the data, the POI's name, address, phone number, longitude and latitude and other information are traversed and output.

3. Code description:

  1. First, we need to replace "Your API Key" in the code with your own API key. This API key can be obtained from the AMAP open platform.
  2. $location is the center point coordinates, and the coordinates of Beijing city center are used here.
  3. $radius is the query radius, in meters. The example here is to query POIs within a radius of 1000 meters.
  4. $keywords are keywords and can be set as needed. For example, querying food can be set to "food".
  5. $types are POI types, such as catering services, banks, etc. If the type is not restricted, it can be left blank.
  6. $offset and $page are the number of results displayed on each page and the current page number respectively. By default, 20 results are displayed on each page, which can be adjusted as needed.

4. Summary:

Through the above code example, we can use the Amap API in PHP to easily obtain detailed information about surrounding POIs. This provides convenience for us to develop various geographical location-based applications, such as map navigation, nearby recommendations, etc. I believe that this development method combined with the Amap API will bring more convenience and innovation to our lives in the future.

The above is the detailed content of How to use Amap API in php to obtain detailed information of surrounding POIs. 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