Autonavi API address lbs.amap.com/api/webserv…

Amap API

Utility class

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; /** * Network requests (GET and POST) * @filename httprequest.java * @class name HttpRequest */ public class HttpRequest {/** * Request to send the GET method to the specified URL ** @param URL Url for sending a request * @param Param request parameters. The request parameters should be in the form of name1=value1&name2=value2. Public static String sendGet(String URL, String param) {String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); URLConnection connection = realurl.openConnection (); / / set the request of the general properties connection. The setRequestProperty (" accept ", "* / *"); connection.setRequestProperty("connection", "Keep-Alive"); Connection. The setRequestProperty (" the user-agent ", "Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // Establish the actual connection connection.connect(); / / get all the response header field Map < String, a List < String > > Map = connection. GetHeaderFields (); For (String key: map.keyset ()) {// system.out.println (key + "-- >" + map.get(key)); } / / define BufferedReader response in the input stream to read the URL = new BufferedReader (new InputStreamReader (connection. The getInputStream ())); String line; while ((line = in.readLine()) ! = null) { result += line; }} catch (Exception e) {system.out.println + e); e.printStackTrace(); } // Use the finally block to close the input stream finally {try {if (in! = null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** ** Send a POST request to the specified URL ** @param URL URL to send the request * @param Param request parameters, which should be in the form of name1=value1&name2=value2. Public static String sendPost(String URL, String param) {PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realurl.openConnection (); // Set the generic request property conn.setrequestProperty (" Accept ", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); Conn. SetRequestProperty (" the user-agent ", "Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // To send a POST request, the following two lines must be set: conn.setdoOutput (true); conn.setDoInput(true); Out = new PrintWriter(conn.getOutputStream()); Out.print (param); // Flush the buffer of the output stream out.flush(); In = new BufferedReader(new InputStreamReader(conn.getinputStream ())); String line; while ((line = in.readLine()) ! = null) { result += line; }} catch (Exception e) {system.out.println (" error sending POST request! + e); e.printStackTrace(); } // Use finally blocks to close output streams, input streams finally {try {if (out! = null) { out.close(); } if (in ! = null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }}Copy the code
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class MapNavUtil { private static final String key="6c7f9d543d1a2c0afbcfaa00c862804c"; Public AddressGet getResultsName(String location){String sendGet = HttpRequest.sendGet("http://restapi.amap.com/v3/geocode/regeo", "key="+key+"&location="+location); JSONObject jsonObject= JSONObject.parseObject(sendGet); JSONObject jsonArray = jsonObject.getJSONObject("regeocode"); AddressGet addressGet=new AddressGet(); addressGet.setAddress(jsonArray.get("formatted_address").toString()); return addressGet; } public AddressGet getResults(String startCoordinate,String endCoordinate){ String sendGet = HttpRequest.sendGet("https://restapi.amap.com/v3/direction/driving", "key="+key+"&origin="+startCoordinate+"&destination="+endCoordinate); JSONObject jsonObject= JSONObject.parseObject(sendGet); String routeJsonString = jsonObject.get("route").toString(); JSONObject routeObject=JSONObject.parseObject(routeJsonString); JSONArray jsonArray = routeObject.getJSONArray("paths"); AddressGet addressGet=null; if (jsonArray! =null&&jsonArray.size()! =0){ JSONObject zuiJson = jsonArray.getJSONObject(0); addressGet=new AddressGet(); addressGet.setDistance(zuiJson.get("distance").toString()); addressGet.setDuration(zuiJson.get("duration").toString()); } return addressGet; } public AddressGet getResultsCoord(String address){String sendGet = HttpRequest.sendGet("https://restapi.amap.com/v3/geocode/geo","key="+key+"&address="+address); JSONObject jsonObject= JSONObject.parseObject(sendGet); JSONArray jsonArray = jsonObject.getJSONArray("geocodes"); AddressGet addressGet=new AddressGet();; if (jsonArray! =null&&jsonArray.size()! =0){ JSONObject zuiJson = jsonArray.getJSONObject(0); addressGet.setCoord(zuiJson.get("location").toString()); } return addressGet; } public static void main(String[] args) { MapNavUtil mapNavUtil=new MapNavUtil(); AddressGet Results = mapNavUtil. GetResults ("116.39657,39.90844", "116.428499,39.93635"); Println (" distance in meters "+results.getDistance()); system.out.println (" distance in meters "+results.getDistance()); System.out.println(" time in seconds "+ results.getduration ()); AddressGet coord = mapNavUtil. GetResultsCoord (" DongHuaMen street in dongcheng district in Beijing zhongshan park "); System.out.println(" coord.getCoord()); AddressGet resultsName = mapNavUtil. GetResultsName (116.39657, 39.90844, ""); System.out.println(" resultsName.getAddress() "); }}Copy the code

Entity class

public class AddressGet { private String address,coord,distance,duration,endCoordinate,startCoordinate; // omit SetGet method}Copy the code