A recent project requires me to calculate the distance between two places, not a straight distance.
That must use the relevant map API Ah, I chose the Baidu map
I have set the incoming parameters of two, respectively, the origin and destination of the district (county) name
The first is to use JS SDK to calculate, but the network requirements a little high, network stability can be calculated, instability will not be calculated, by looking at Baidu Map development API documents, I chose the way through the HTTP interface
First on the Internet to find a package of good httpclientutil
public class Httpclientutil {public static string doget (String URL, map<string, string> param) {//Chuang
Build HttpClient object Closeablehttpclient httpclient = Httpclients.createdefault ();
String resultstring = "";
Closeablehttpresponse response = null;
try {//create URI UriBuilder builder = new UriBuilder (URL); if (param!= null) {for (String Key:param.keySet ()) {Builder.addparameter (key, par
Am.get (key));
} uri uri = Builder.build ();
Creates an HTTP GET request HttpGet HttpGet = new HttpGet (URI);
Execution Request response = Httpclient.execute (HttpGet); Determines whether the return status is Response.getstatusline (). Getstatuscode () = = Resultstring = Entityut
Ils.tostring (Response.getentity (), "UTF-8"); } catch (Exception e) {E.printstacktrace();
Finally {try {if (response!= null) {response.close ();
} httpclient.close ();
catch (IOException e) {e.printstacktrace ();
} return resultstring;
public static string doget (string url) {return doget (URL, null); public static string doPost (String URL, map<string, string> param) {//Create HttpClient Object Closea
Blehttpclient httpclient = Httpclients.createdefault ();
Closeablehttpresponse response = null;
String resultstring = "";
try {//create HTTP POST request HttpPost HttpPost = new HttpPost (URL);
Create a parameter list if (param!= null) {list<namevaluepair> paramlist = new arraylist<> (); For (String Key:param.keySet ()) {Paramlist.add (new Basicnamevaluepair key, param.geT (key));
///analog form urlencodedformentity entity = new urlencodedformentity (paramlist);
Httppost.setentity (entity);
//execute HTTP request response = Httpclient.execute (HttpPost);
resultstring = entityutils.tostring (Response.getentity (), "utf-8");
catch (Exception e) {e.printstacktrace ();
Finally {try {response.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} return resultstring;
public static string DoPost (string url) {return doPost (URL, null); public static string Dopostjson (string URL, string json) {///Create HttpClient Object Closeablehttpclient HT
Tpclient = Httpclients.createdefault ();
Closeablehttpresponse response = null; String resultstring = "";
try {//create HTTP POST request HttpPost HttpPost = new HttpPost (URL);
Create request content stringentity entity = new Stringentity (JSON, Contenttype.application_json);
Httppost.setentity (entity);
Execute HTTP Request response = Httpclient.execute (HttpPost);
resultstring = entityutils.tostring (Response.getentity (), "utf-8");
catch (Exception e) {e.printstacktrace ();
Finally {try {response.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} return resultstring;
}
}
The following is my own writing to calculate the distance between the two util:
public class Calculatedisutil {//origin: Origin place name, such as: Yuhang//destination: Destination place name, such as: Huangpu//Return to the distance between the two places, such as: 234.56 km pub Lic static string distance (string origin, string destination) {map<string, string> params = new hashmap<&
gt; (); String origindouble = Httpclientutil.doget ("http://api.map.baidu.com/geocoder/v2/?output=json&ak= your key&
Address= "+ origin); String desdouble = Httpclientutil.doget ("http://api.map.baidu.com/geocoder/v2/?output=json&ak= your key&
address= "+ destination);
Com.alibaba.fastjson.JSONObject Jsonobjectori = Com.alibaba.fastjson.JSONObject.parseObject (origindouble);
Com.alibaba.fastjson.JSONObject jsonobjectdes = Com.alibaba.fastjson.JSONObject.parseObject (desdouble);
String orilng = jsonobjectori.getjsonobject ("result"). Getjsonobject ("location"). GetString ("LNG");
String Orilat = jsonobjectori.getjsonobject ("result"). Getjsonobject ("location"). GetString ("lat"); System.out.printlN (orilat+ "|")
+ORILNG);
String deslng = jsonobjectdes.getjsonobject ("result"). Getjsonobject ("location"). GetString ("LNG");
String Deslat = jsonobjectdes.getjsonobject ("result"). Getjsonobject ("location"). GetString ("lat"); System.out.println (deslat+ "|")
+DESLNG);
Params.put ("Output", "json");
Params.put ("AK", "your Key"); Params.put ("Origins", orilat+ "," +orilng+ "|"
+orilat+ "," +orilng); Params.put ("Destinations", deslat+ "," +deslng+ "|"
+deslat+ "," +deslng);
String result = Httpclientutil.doget ("http://api.map.baidu.com/routematrix/v2/driving", params);
Jsonarray Jsonarray = com.alibaba.fastjson.JSONObject.parseObject (Result)-Getjsonarray ("result");
String distancestring = jsonarray.getjsonobject (0). Getjsonobject ("Distance"). GetString ("text");
return distancestring;
}
}
For the parameters of the above request URL, just take a look at the service documentation for the batch count Route