Home > Java > javaTutorial > In Baidu Map API, how to search and display location coordinates based on address using Java?

In Baidu Map API, how to search and display location coordinates based on address using Java?

WBOY
Release: 2023-07-29 09:07:54
Original
1794 people have browsed it

In Baidu Map API, how to use Java to search and display location coordinates based on address?

When developing map-related applications, it is often necessary to search and display location coordinates based on address information. Baidu Maps provides a rich API interface, including the function of searching based on address. This article will introduce how to use Java code to implement the function of searching and displaying location coordinates based on address in Baidu Map API.

First of all, we need to prepare some necessary information, including Baidu Map Developer Key (AK) and the address to be searched. Developer keys can be obtained on the Baidu Map Open Platform. Next, we need to introduce relevant Java libraries, including the Apache HttpClient library and JSON parsing library, for HTTP requests and data parsing.

The following is a simple Java code example that demonstrates how to use the Baidu Map API to search and display location coordinates based on an address:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

public class MapSearchDemo {

    public static void main(String[] args) {
        String address = "北京市海淀区中关村";
        String ak = "your_baidu_map_ak";

        try {
            // 构建URL
            String urlString = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + ak;
            URL url = new URL(urlString);

            // 发起HTTP请求
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 解析JSON数据
            JSONObject jsonObject = new JSONObject(response.toString());
            int status = jsonObject.getInt("status");
            if (status == 0) {
                JSONArray results = jsonObject.getJSONArray("result");
                if (results.length() > 0) {
                    JSONObject location = results.getJSONObject(0).getJSONObject("location");

                    // 提取经纬度
                    double lng = location.getDouble("lng");
                    double lat = location.getDouble("lat");

                    System.out.println("地址:" + address);
                    System.out.println("经度:" + lng);
                    System.out.println("纬度:" + lat);
                } else {
                    System.out.println("未找到相关地址信息!");
                }
            } else {
                System.out.println("请求失败,错误码:" + status);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In this example, we first specify the address to be searched and Baidu Maps Developer Key (AK). Using Java's URL and HttpURLConnection classes, we construct an HTTP request and send it to the Baidu Map API. Then, we extracted the longitude and latitude information of the location by parsing the JSON data returned by the API. Finally, we print the results.

Please note that during actual use, you need to replace your_baidu_map_ak in the sample code with your own Baidu Map developer key.

To summarize, by using Baidu Map API and Java code, we can easily search and display location coordinates based on addresses. This feature is very useful in developing map-related applications. Hope this article can be helpful to you!

The above is the detailed content of In Baidu Map API, how to search and display location coordinates based on address 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