Home  >  Article  >  Java  >  In Baidu Map API, how to use Java to obtain subway line information at a specified location?

In Baidu Map API, how to use Java to obtain subway line information at a specified location?

WBOY
WBOYOriginal
2023-07-30 16:31:511711browse

In Baidu Map API, how to use Java to obtain subway line information at a specified location?

The subway plays an important role in modern urban transportation. To facilitate users to find subway line information, Baidu Maps provides corresponding API interfaces. This article will introduce how to use Java code to obtain subway line information at a specified location through Baidu Map API.

First, we need to obtain the Baidu Map developer key. After registering and logging in to the Baidu Map Open Platform, create a new application on the "Application Management" page, and then obtain the developer key in "Key Management". After obtaining the key, you can use the key to make subsequent API calls.

Next, we need to import the relevant Java libraries. We can use the Java development package management tool Maven to manage dependencies. Add the following dependencies to the project's pom.xml file:

<dependencies>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
  </dependency>
  <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.9</version>
  </dependency>
</dependencies>

In the code, we first need to construct an HTTP request and set the requested URL, request parameters and other information. Then send an HTTP request and get the return result. Finally, the returned results are parsed and the required subway line information is extracted.

The following is a sample code that demonstrates how to use Java to obtain subway line information at a specified location:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class BaiduMapAPI {
    
    public static void main(String[] args) {
        String url = "http://api.map.baidu.com/place/v2/search?query=地铁站&location=40.057406,116.296440&radius=2000&output=json&ak=Your_AK";
        
        String result = sendHttpRequest(url);
        Map<String, String> subwayLines = parseSubwayLines(result);
        
        System.out.println("地铁线路信息:");
        for (String lineName : subwayLines.keySet()) {
            System.out.println(lineName + ":" + subwayLines.get(lineName));
        }
    }
    
    private static String sendHttpRequest(String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
                in.close();
                
                return response.toString();
            } else {
                System.out.println("HTTP请求失败,错误码:" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    private static Map<String, String> parseSubwayLines(String json) {
        Map<String, String> subwayLines = new HashMap<>();
        JSONObject jsonObject = JSON.parseObject(json);
        
        JSONArray results = jsonObject.getJSONArray("results");
        for (int i = 0; i < results.size(); i++) {
            JSONObject result = results.getJSONObject(i);
            
            String name = result.getString("name");
            String detail = result.getString("detail_info");
            
            subwayLines.put(name, detail);
        }
        
        return subwayLines;
    }
}

In the above code, you need to replace Your_AK with your own Baidu Map API developer key. locationThe parameters are used to specify the longitude and latitude coordinates of a certain location. The coordinates of Beijing are used here. radiusThe parameter is used to specify the radius of the search area, here it is set to 2000 meters. The query parameter is used to specify the search keyword, here "subway station" is used.

Run the above code to output the subway line information at the specified location on the console.

Through the above code example, we can see how to use Java code to call Baidu Map API to obtain subway line information at a specified location. You can also obtain other types of map data through Baidu Map API according to actual needs. I hope this article can provide some help for you to understand and use Baidu Map API.

The above is the detailed content of In Baidu Map API, how to use Java to obtain subway line information at a specified location?. 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