Home >Java >javaTutorial >How to call Amap API through Java code to implement path distance calculation
How to call Amap API through Java code to implement path distance calculation
As people’s demand for real-time traffic conditions and navigation increases, map route planning becomes more and more important. As the leading map service provider in China, Amap's route planning function is favored by the majority of developers. This article will introduce how to call the Amap API through Java code to implement path distance calculation.
Amap API provides a series of rich interfaces, including geocoding, reverse geocoding, route planning and other functions. In this article, we will focus on how to use the Amap API to calculate path distance.
Before we start, we need to prepare some necessary work:
After the preparation work is completed, we can start writing Java code. First, we need to create a Java project and introduce the Amap API jar package into the project.
Next, we can create a class, such as PathDistanceCalculator
, as a tool class for path distance calculation. This tool class will provide a method to receive the latitude and longitude coordinates of the starting point and the end point, calculate the path distance between the two points through the Amap API, and return the result.
The sample code is as follows:
import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; public class PathDistanceCalculator { private static final String AMAP_API_KEY = "your_amap_key"; public static double calculateDistance(double startLng, double startLat, double endLng, double endLat) { String apiUrl = "https://restapi.amap.com/v3/distance"; String url = String.format("%s?key=%s&origins=%f,%f&destination=%f,%f", apiUrl, AMAP_API_KEY, startLng, startLat, endLng, endLat); try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { HttpGet request = new HttpGet(url); HttpResponse response = httpClient.execute(request); String resultJson = EntityUtils.toString(response.getEntity()); JSONObject jsonObject = JSONObject.parseObject(resultJson); List<Map<String, Object>> results = jsonObject.getJSONArray("results").toJavaList(HashMap.class); Map<String, Object> distanceInfo = results.get(0); return Double.parseDouble(distanceInfo.get("distance").toString()); } catch (IOException e) { e.printStackTrace(); return -1; } } public static void main(String[] args) { double startLng = 116.397477; double startLat = 39.908692; double endLng = 121.472644; double endLat = 31.231706; double distance = calculateDistance(startLng, startLat, endLng, endLat); System.out.println("距离:" + distance + "米"); } }
In the sample code, we first set the longitude and latitude coordinates of the starting point and the end point, and then call the calculateDistance
method to calculate the distance between the two points path distance between them and print the result.
It should be noted that AMAP_API_KEY
in the sample code needs to be replaced with your own AMAP API key.
Compile and run the sample code, you will get the path distance between two points.
Through the above steps, we successfully used Java code to call the Amap API to calculate the path distance. You can apply it to specific projects according to your needs.
Summary:
This article introduces how to call the Amap API through Java code to calculate the path distance. First, we provide readers with preparation work, including obtaining the Amap developer account and API key, and importing the necessary development tools. Then, we wrote a PathDistanceCalculator
tool class to calculate the path distance between two points using the Amap API. Finally, we give a complete example code and explain it. I hope this article can be helpful to readers, and that readers can develop richer and more practical map applications through the knowledge learned in this article.
The above is the detailed content of How to call Amap API through Java code to implement path distance calculation. For more information, please follow other related articles on the PHP Chinese website!