Home > Java > javaTutorial > How to use Java code to find nearby places of a specified type through the Baidu location search API on Baidu Maps?

How to use Java code to find nearby places of a specified type through the Baidu location search API on Baidu Maps?

王林
Release: 2023-07-30 10:13:14
Original
1565 people have browsed it

How to use Java code to find nearby places of a specified type through the Baidu location search API on Baidu Maps?

Baidu Map is a widely used map service that provides a variety of functions and APIs for developers to use. Among them, the location search API can help us search for surrounding locations based on keywords. In this article, I will introduce how to use Java code to call Baidu location search API to find nearby locations of a specified type.

First, we need to register a Baidu developer account and create an application to obtain the API key. After obtaining the API key, we can start writing Java code.

First, we need to create an HttpClient object to send HTTP requests. This can be easily implemented using the Apache HTTP Components library. With Maven, we can add the following dependencies to the pom.xml file:

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
</dependencies>
Copy after login

Next, we need to construct the request URL. The request URL format of Baidu location search API is as follows:

http://api.map.baidu.com/place/v2/search?query=关键词&location=纬度,经度&radius=半径&output=输出格式&ak=密钥
Copy after login

Among them, the query parameter is used to specify the search keyword; the location parameter is used to specify the coordinates of the center point of the search, in the format of "latitude, longitude"; the radius parameter Used to specify the radius of the search, in meters; the output parameter is used to specify the output format, which can be JSON or XML; the ak parameter is used to specify the developer's API key.

Here is a sample code snippet that demonstrates how to construct a request URL:

String query = "餐馆";
double latitude = 39.915;
double longitude = 116.404;
int radius = 1000;
String output = "json";
String ak = "your_api_key";

String url = String.format("http://api.map.baidu.com/place/v2/search?query=%s&location=%f,%f&radius=%d&output=%s&ak=%s",
        URLEncoder.encode(query, "UTF-8"), latitude, longitude, radius, output, ak);
Copy after login

Next, we need to send the HTTP request and parse the returned JSON or XML data. You can use the com.google.gson library to easily parse JSON formatted data. Through Maven, we can add the following dependencies to the pom.xml file:

<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
    </dependency>
</dependencies>
Copy after login

The following is a sample code snippet that demonstrates how to send an HTTP request and parse the returned JSON data:

HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String responseString = EntityUtils.toString(httpEntity);

JsonObject responseJson = JsonParser.parseString(responseString).getAsJsonObject();
JsonArray resultsArray = responseJson.getAsJsonArray("results");

for (JsonElement result : resultsArray) {
    JsonObject resultObject = result.getAsJsonObject();
    String name = resultObject.get("name").getAsString();
    JsonObject locationObject = resultObject.getAsJsonObject("location");
    double resultLatitude = locationObject.get("lat").getAsDouble();
    double resultLongitude = locationObject.get("lng").getAsDouble();
    System.out.println(name + ": " + resultLatitude + ", " + resultLongitude);
}
Copy after login

Above The code snippet extracts the name and coordinates of each location from the returned JSON data and outputs it to the console.

Finally, we only need to integrate the above code into a Java class and execute the main function to run the code. In actual use, it can be appropriately modified and optimized according to needs.

Through the above steps, we can use Java code to find the nearby places of the specified type through the Baidu location search API on Baidu Maps. Hope this article is helpful to you!

The above is the detailed content of How to use Java code to find nearby places of a specified type through the Baidu location search API on Baidu Maps?. For more information, please follow other related articles on the PHP Chinese website!

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