Home >Backend Development >PHP Tutorial >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
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:
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:
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!