In Baidu Map API, how to use Java to obtain surrounding life service information at a specified location?
Baidu Map API is an open platform that provides maps, navigation, LBS cloud maps and other functions. Developers can obtain some map-related data through API calls. Among them, obtaining information on surrounding life services at a specified location is one of the most common needs. This article will introduce how to use Java code to implement this function.
First, we need to apply for a developer account on the Baidu Developer Platform and create an application. After successfully creating the application, you will get an application ak (Access Key), which is the identity identifier for accessing Baidu Map API.
Next, we need to import the relevant Java library files for network requests and JSON data parsing. In this example, we can use Apache's HttpClient for network requests and Google's Gson library for JSON data parsing. You can add the following dependencies in the project's pom.xml file:
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.7</version> </dependency> </dependencies>
Next, we can write Java code to implement the function of obtaining surrounding life service information at a specified location. The following is a sample code:
import com.google.gson.Gson; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; public class BaiduMapApiExample { private static final String API_URL = "http://api.map.baidu.com/place/v2/search"; private static final String AK = "your_access_key"; public static void main(String[] args) throws IOException { String location = "31.2304,121.4737"; // 指定位置的经纬度 String keyword = "餐馆"; // 搜索关键字 int radius = 2000; // 搜索半径(单位:米) String url = String.format("%s?query=%s&location=%s&radius=%d&output=json&ak=%s", API_URL, URLEncoder.encode(keyword, StandardCharsets.UTF_8), location, radius, AK); HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); String response = EntityUtils.toString(httpClient.execute(httpGet).getEntity()); Gson gson = new Gson(); ApiResponse apiResponse = gson.fromJson(response, ApiResponse.class); for (Place place : apiResponse.getResults()) { System.out.println("名称:" + place.getName()); System.out.println("地址:" + place.getAddress()); System.out.println("电话:" + place.getTelephone()); System.out.println("--------------------------"); } } private static class ApiResponse { private Place[] results; public Place[] getResults() { return results; } } private static class Place { private String name; private String address; private String telephone; public String getName() { return name; } public String getAddress() { return address; } public String getTelephone() { return telephone; } } }
In the above code, we first construct a URL, which contains the necessary parameters, such as query keywords, location, radius and access key. Then, we use HttpClient to send an HTTP GET request and get the returned JSON string.
Next, we use the Gson library to parse the JSON string into Java objects. In this example, we define two classes, ApiResponse
and Place
, to correspond to the returned JSON data structure. Finally, we traverse the parsed Place
objects and print relevant information.
The above is an example of using Java code to obtain surrounding life service information at a specified location. By calling Baidu Map API, we can obtain rich geographical information to meet different application needs. You can modify the code according to your needs to adapt to more scenarios.
The above is the detailed content of In Baidu Map API, how to use Java to obtain surrounding life service information at a specified location?. For more information, please follow other related articles on the PHP Chinese website!