Home > Java > Java Tutorial > body text

In Baidu Map API, how to get POI (Point of Interest) details of a specified location using Java?

WBOY
Release: 2023-07-29 23:46:49
Original
1764 people have browsed it

In modern society, people are becoming more and more aware of and familiar with their surrounding environment, one of which is knowing nearby points of interest (POI). POI refers to some specific geographical locations, such as restaurants, hotels, tourist attractions, etc. Baidu Maps provides a powerful API to help developers obtain POI details at a specified location. This article will introduce how to use Java language to implement this function.

First, we need to apply for a Baidu Maps developer account and obtain the corresponding API key. After obtaining the API key, we can start writing Java code to obtain POI details at the specified location.

First, we import the necessary Java classes and libraries:

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
Copy after login

Then, we can define a method to get the POI details:

public static void getPOIDetails(String location, String query, String ak) {
    try {
        // 构造URL
        String urlString = "http://api.map.baidu.com/place/v2/search?query=" + query + "&location=" + location + "&radius=2000&output=json&ak=" + ak;
        URL url = new URL(urlString);
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 设置请求参数
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");
        // 获取响应结果
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        // 解析JSON结果
        JSONObject jsonObject = JSONObject.fromObject(response.toString());
        JSONArray results = jsonObject.getJSONArray("results");
        // 输出POI详细信息
        for (int i = 0; i < results.size(); i++) {
            JSONObject result = results.getJSONObject(i);
            System.out.println("POI名称:" + result.getString("name"));
            System.out.println("POI地址:" + result.getString("address"));
            System.out.println("POI电话:" + result.getString("telephone"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Copy after login

In the above code, we Concatenate request parameters by constructing a URL, where query represents the POI type we want to query (such as restaurants, hotels, etc.), location represents the location coordinates to be queried, radius represents the query radius (unit: meters), output represents the output data format, ak represents the Baidu Map developer API key.

Finally, we can call the above method in the main method. The example is as follows:

public static void main(String[] args) {
    // 要查询的位置坐标
    String location = "39.915,116.404";
    // 要查询的POI类型
    String query = "餐馆";
    // 百度地图开发者API密钥
    String ak = "Your API Key";
    // 获取POI详细信息
    getPOIDetails(location, query, ak);
}
Copy after login

In the above example, we pass the given location coordinates (taking Beijing as an example) and POI type ( restaurant), call the getPOIDetails method to obtain the POI details of the specified location.

Through the above code, we can easily use Java language to obtain the POI details of the specified location in Baidu Maps. This is very useful for many application scenarios, such as surrounding search, navigation, etc. Hope this article helps you!

The above is the detailed content of In Baidu Map API, how to get POI (Point of Interest) details of a specified location using Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!