Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-07-31 19:09:39
Original
1008 people have browsed it

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

Baidu Map API provides a powerful function, which is to obtain road information through longitude and latitude. In Java, we can use HttpURLConnection and BufferedReader to send HTTP GET requests and parse the returned JSON data to obtain road information at a specified location.

First, we need to introduce the relevant library files:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
Copy after login

Then, we define a method to obtain the road information at the specified location:

public static String getRoadInfo(double lat, double lng) {
    String roadInfo = "";
    
    try {
        // 创建URL对象并构建请求URL
        URL url = new URL("http://api.map.baidu.com/road/v1/nearest_roads?ak=your_ak&coord_type=wgs84ll&point=" + lat + "," + lng);

        // 创建HttpURLConnection对象并打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        // 获取返回数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 解析JSON数据
        JSONObject jsonObject = new JSONObject(response.toString());
        JSONArray roadArray = jsonObject.getJSONArray("roads");
        if (roadArray.length() > 0) {
            roadInfo = roadArray.getJSONObject(0).getString("name");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return roadInfo;
}
Copy after login

In the above code, we The "nearest_roads" interface in Baidu Map Web Service API is used, which can return the route information closest to the specified longitude and latitude. Please note that "ak=your_ak" in the URL must be replaced with your own Baidu Maps API key.

Next, we can call the above method in the main method to obtain the road information at the specified location:

public static void main(String[] args) {
    double lat = 39.983424;// 纬度
    double lng = 116.322987;// 经度
    
    String roadInfo = getRoadInfo(lat, lng);
    System.out.println("道路信息:" + roadInfo);
}
Copy after login

The latitude and longitude in the above code is the location of the center of Beijing. You can modify the latitude and longitude to get road information for other locations.

Through the above code, we can use Baidu Map API in Java to obtain road information at a specified location. This is very useful for some application scenarios that require path planning, positioning analysis, etc. based on road information.

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