A,MapB) {
//System.out.println("目标距离计算开始...");
try {
URL resjson = new URL
("https://restapi.amap.com/v3/direction/driving?&output=JSON&extensions=all&origin="+A.get("lng")+","+A.get("lat")+"&destination="+B.get("lng")+","+B.get("lat")+"&key="+GAODE_AK);
BufferedReader in = new BufferedReader(new InputStreamReader(
resjson.openStream()));
String res;
StringBuilder sb = new StringBuilder("");
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
in.close();
String str = sb.toString();
//System.out.println(str);
if(str!=null&&!str.equals("")){
double temp=0;
int disStart = str.indexOf("distance\":");
//System.out.println(disStart);
int disEnd = str.indexOf(",\"duration");
//System.out.println(disEnd);
if (disStart > 0 && disEnd > 0) {
//System.out.println("距离计算开始...");
String dis = str.substring(disStart + 11, disEnd-1);
//System.out.println(dis);
temp=format(dis);
//System.out.println(temp);
if(temp<=1.5)
{
temp=0;
//System.out.println("距离=0已经返回");
return temp;
}else {
//System.out.println("距离已经返回");
return temp;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println("error happens!");
return (Double) null;
}
public static void main(String args[])
{
double temp=backDis(getLatitude("杭州"),getLatitude("上海"));
System.out.println(temp);
}
}