In Baidu Map API, how to use Java to obtain nearby attraction information at a specified location?
With the development of tourism, people have become more convenient and pursue personalized information about travel destinations. There is often a need to obtain nearby attraction information at a specific location so that it can be provided to users. This function can be easily implemented using Baidu Map API. In this article, I will introduce how to use Java programming language to obtain nearby attractions information at a specified location in Baidu Map API.
First of all, we need to prepare a Baidu Map developer account and obtain the Baidu Map AK (Access Key).
Next, we need to introduce the relevant Java libraries. In this example, we use the HttpClient library to make HTTP requests and the Json library to process the returned JSON data.
import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject;
Then, we need to define a method to send an HTTP request, which will take the URL of the nearby attraction information as a parameter and return the response JSON string.
public static String sendGetRequest(String url) throws ClientProtocolException, IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity); return result; }
Next, we need to write a method to parse the returned JSON data and extract information about nearby attractions.
public static void getNearbyPlaces(String location) { String ak = "YOUR_AK_HERE"; String url = "http://api.map.baidu.com/place/v2/search?query=景点&location=" + location + "&radius=2000&output=json&ak=" + ak; try { String result = sendGetRequest(url); JSONObject jsonObject = JSON.parseObject(result); JSONArray results = jsonObject.getJSONArray("results"); for (int i = 0; i < results.size(); i++) { JSONObject place = results.getJSONObject(i); String name = place.getString("name"); String address = place.getString("address"); double lng = place.getJSONObject("location").getDouble("lng"); double lat = place.getJSONObject("location").getDouble("lat"); System.out.println("名称:" + name); System.out.println("地址:" + address); System.out.println("经度:" + lng); System.out.println("纬度:" + lat); System.out.println("-----------------------------------"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
In the above code, we first construct the URL and add AK, query type (query), latitude and longitude (location), search radius (radius), and return format (output) as parameters to the URL. Then, we send an HTTP request to obtain the returned JSON data, and extract the name, address, longitude and latitude of nearby attractions through JSON parsing. Finally, we print this information.
Finally, we can call the getNearbyPlaces method and pass in the longitude and latitude of the specified location to obtain information about nearby attractions.
public static void main(String[] args) { String location = "116.404,39.915"; getNearbyPlaces(location); }
The above is a simple example of using Java to obtain information about attractions near a specified location in Baidu Map API. Through this example, we can easily implement the function of obtaining information about nearby attractions and provide users with more personalized and accurate travel services. Hope this article is helpful to you!
The above is the detailed content of In Baidu Map API, how to use Java to obtain nearby attraction information at a specified location?. For more information, please follow other related articles on the PHP Chinese website!