Java调用高德Api

登陆高德API官网,注册,申请Key

官网地址:http://lbs.amap.com

Elipse 输入下面代码

注意要在URL上输入自己申请的Key

package com.saiita.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.net.URL;import java.nio.charset.Charset;import org.json.JSONException;import org.json.JSONObject;public class Test {	private static String readAll(Reader rd) throws IOException {		StringBuilder sb = new StringBuilder();			int cp;			while ((cp = rd.read()) != -1) {			sb.append((char) cp);		}			return sb.toString();		}			public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {			InputStream is = new URL(url).openStream();				try {				BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));			String jsonText = readAll(rd);			JSONObject json = new JSONObject(jsonText);			return json;		}		finally {				is.close();		// System.out.println("同时 从这里也能看出 即便return了,仍然会执行finally的!");		}				}			public static void main(String[] args) throws IOException, JSONException {			//这里调用高德的驾车规划api服务 详见 http://lbs.amap.com/api/webservice/summary/				JSONObject json = readJsonFromUrl("http://restapi.amap.com/v3/direction/walking?key=申请的Key&origin=116.481028,39.989644&destination=116.434446,39.90816");			System.out.println(json.toString());				System.out.println(((String) json.get("info")));		}		}

运行

就会得到下面结果

{"route":{"paths":[{"duration":"8999","distance":"11249","steps":[{"duration":"10","assistant_action":[],"orientation":"西南","distance":"13","road":[],"instruction":"向西南步行13米左转","action":"左转","walk_type":"0","polyline":"116.481003,39.989311;116.480957,39.989265;116.480957,39.989265;116.480904,39.98922"},{"duration":"240","assistant_action":[],"orientation":"东南","distance":"300","road":"阜荣街","instruction":"沿阜荣街向东南步行300米右转","action":"右转","walk_type":"0","polyline":"116.480904,39.989212;116.480949,39.989185;116.481247,39.988991;116.481415,39.988888;116.481415,39.988888;116.481445,39.988865;116.481445,39.988865;116.482567,39.988125;116.483414,39.987583;116.483467,39.987404"},{"duration":"246","assistant_action":[],"orientation":"西南","distance":"308","road":"望京东路","instruction":"沿望京东路向西南步行308米左转","action":"左转","walk_type":"0","polyline":"116.483467,39.9874;116.483414,39.987354;116.483414,39.987354;116.482811,39.986816;116.482025,39.986088;116.482025,39.986088;116.481354,39.985489;116.481354,39.985489;116.481285,39.985435;116.481285,39.985435;116.481148,39.985275"},{"duration":"130","assistant_action":[],"orientation":"东南","distance":"162","road":"广顺南大街","instruction":"沿广顺南大街向东南步行162米右转","action":"右转","walk_type":"0","polyline":"116.481148,39.985271;116.481247,39.985203;116.481247,39.985203;116.48127,39.985092;116.482162,39.984493;116.482162,39.984493;116.482498,39.984257"},{"duration":"1022","assistant_action":[],"orientation":"西南","distance":"1278","road":"京密路","instruction":"沿京密路向西南步行1278米向右前方行走","action":"向右前方行走","walk_type":"0","polyline":"116.482498,39.984253;116.48246,39.984219;116.48217,39.983974;116.481659,39.983494;116.481659,39.983494;116.481239,39.983086;116.480728,39.982643;116.479652,39.981667;116.479652,39.981667;116.478668,39.980797;116.478096,39.98027;116.478096,39.98027;116.47802,39.980206;116.47802,39.980206;116.477982,39.980164;116.477982,39.980164;116.477699,39.979889;116.477585,39.979782;116.477211,39.979435;116.477211,39.979435;116.476852,39.979115;116.476852,39.979115;116.47673,39.979;116.47673,39.979;116.476044,39.97839;116.476044,39.97839;116.475113,39.977551;116.475113,39.977551;116.475105,39.977543;116.475105,39.977543;116.475029,39.977482;116.475029,39.977482;116.474312,39.976822;116.474312,39.976822;116.473885,39.976433;116.473885,39.976433;116.473289,39.975895;116.473289,39.975895;116.472763,39.97543"},{"duration":"302","assistant_action":[],"orientation":"西南","distance":"377","road":"京密路辅路","instruction":"沿京密路辅路向西南步行377米直行","action":"直行","walk_type":"0","polyline":"116.472763,39.975426;116.472214,39.975147;116.471886,39.974888;116.47139,39.974434;116.47139,39.974434;116.471161,39.97427;116.470993,39.974159;116.470642,39.973961;116.470345,39.973831;116.469994,39.97369;116.46978,39.973633;116.469208,39.973507"},{"duration":"290","assistant_action":[],"orientation":"西南","distance":"362","road":"北四环东路辅路","instruction":"沿北四环东路辅路向西南步行362米直行","action":"直行","walk_type":"0","polyline":"116.469208,39.973503;116.468994,39.973499;116.468834,39.973476;116.468613,39.973438;116.468498,39.973408;116.468399,39.973358;116.468285,39.973282;116.467834,39.972916;116.467834,39.972916;116.467033,39.972218;116.467033,39.972218;116.466705,39.971893;116.466637,39.971806;116.466614,39.971741;116.466576,39.971649;116.466568,39.971561;116.466568,39.971264"},{"duration":"254","assistant_action":[],"orientation":"南","distance":"318","road":"京密路辅路","instruction":"沿京密路辅路向南步行318米直行","action":"直行","walk_type":"0","polyline":"116.466568,39.971252;116.466553,39.971039;116.466522,39.970844;116.466461,39.970634;116.466354,39.970364;116.466278,39.970181;116.46611,39.969921;116.46611,39.969921;116.465965,39.96973;116.46579,39.969513;116.46563,39.969357;116.465599,39.969318;116.465599,39.969318;116.465523,39.969196;116.465446,39.969044;116.465225,39.968624"},{"duration":"1108","assistant_action":[],"orientation":"西南","distance":"1385","road":"京密路","instruction":"沿京密路向西南步行1385米直行","action":"直行","walk_type":"0","polyline":"116.465218,39.968613;116.464951,39.968372;116.464951,39.968372;116.464783,39.968212;116.464783,39.968212;116.464462,39.96793;116.464462,39.96793;116.462814,39.966434;116.462814,39.966434;116.462753,39.966381;116.462753,39.966381;116.4617,39.965416;116.4608,39.964596;116.4608,39.964596;116.460121,39.963982;116.460121,39.963982;116.459999,39.963863;116.459999,39.963863;116.459496,39.963428;116.459412,39.963341;116.459412,39.963341;116.459129,39.963093;116.459129,39.963093;116.458076,39.962139;116.458076,39.962139;116.457954,39.962021;116.457954,39.962021;116.457916,39.96199;116.457916,39.96199;116.457359,39.961475;116.457359,39.961475;116.457314,39.96143;116.457314,39.96143;116.45694,39.961113;116.45694,39.961113;116.456764,39.960964;116.456764,39.960964;116.456657,39.960884;116.456657,39.960884;116.456299,39.96064;116.456299,39.96064;116.456047,39.960472;116.455719,39.960297;116.455536,39.960213;116.455536,39.960213;116.454765,39.959839;116.45459,39.959747;116.454437,39.959652;116.454185,39.95945"},{"duration":"1730","assistant_action":[],"orientation":"西南","distance":"2162","road":"香河园路","instruction":"沿香河园路向西南步行2162米右转","action":"右转","walk_type":"0","polyline":"116.454178,39.959438;116.453575,39.958893;116.453575,39.958893;116.453392,39.958733;116.453392,39.958733;116.453041,39.958412;116.453041,39.958412;116.452881,39.958263;116.452881,39.958263;116.452232,39.95768;116.452232,39.95768;116.452194,39.95763;116.450989,39.956551;116.450989,39.956551;116.45089,39.956463;116.45089,39.956463;116.450874,39.956444;116.450874,39.956444;116.450668,39.956284;116.450668,39.956284;116.450516,39.95615;116.450516,39.95615;116.45034,39.955994;116.45034,39.955994;116.449989,39.955704;116.449989,39.955704;116.44915,39.954922;116.44915,39.954922;116.448853,39.954639;116.448311,39.954174;116.448311,39.954174;116.447617,39.95364;116.447617,39.95364;116.447571,39.953609;116.447571,39.953609;116.447014,39.953205;116.447014,39.953205;116.445847,39.952312;116.445847,39.952312;116.445297,39.951935;116.445297,39.951935;116.444717,39.951523;116.444717,39.951523;116.444145,39.951138;116.444145,39.951138;116.442566,39.949978;116.442566,39.949978;116.442207,39.949703;116.442207,39.949703;116.441216,39.948971;116.441216,39.948971;116.441132,39.948906;116.441132,39.948906;116.440948,39.948765;116.440498,39.94838;116.440498,39.94838;116.439949,39.947922;116.439949,39.947922;116.439552,39.94759;116.439209,39.947269;116.439209,39.947269;116.438995,39.947048;116.438995,39.947048;116.438843,39.94688;116.438843,39.94688;116.438576,39.946587;116.438576,39.946587;116.438469,39.946449;116.438469,39.946449;116.43821,39.946163;116.43821,39.946163;116.437927,39.945789;116.437683,39.945435;116.437347,39.944893;116.437347,39.944893;116.43734,39.944847"},{"duration":"226","assistant_action":[],"orientation":"西","distance":"283","road":"香河园路","instruction":"沿香河园路向西步行283米左转","action":"左转","walk_type":"0","polyline":"116.437332,39.944839;116.435966,39.944836;116.434563,39.944839;116.434563,39.944839;116.434448,39.944839;116.434448,39.944839;116.434006,39.944832"},{"duration":"279","assistant_action":[],"orientation":"南","distance":"349","road":"东直门北大街","instruction":"沿东直门北大街向南步行349米左转","action":"左转","walk_type":"0","polyline":"116.433998,39.944828;116.434052,39.944759;116.434052,39.944431;116.434052,39.944431;116.434052,39.944336;116.434052,39.944336;116.434044,39.943916;116.434044,39.943916;116.434036,39.943382;116.434036,39.943302;116.433945,39.943222;116.433945,39.943222;116.433914,39.943146;116.433914,39.943108;116.433899,39.942921;116.433914,39.942612;116.433914,39.942612;116.433952,39.941711"},{"duration":"33","assistant_action":[],"orientation":[],"distance":"41","road":[],"instruction":"步行41米右转","action":"右转","walk_type":"20","polyline":"116.433945,39.941708;116.434052,39.941708;116.434059,39.94141"},{"duration":"36","assistant_action":[],"orientation":"西","distance":"45","road":"东直门桥","instruction":"沿东直门桥向西步行45米右转","action":"右转","walk_type":"0","polyline":"116.434059,39.941406;116.433868,39.941406;116.433525,39.941399"},{"duration":"27","assistant_action":[],"orientation":[],"distance":"34","road":[],"instruction":"步行34米右转","action":"右转","walk_type":"20","polyline":"116.433517,39.941395;116.43351,39.941662;116.433556,39.941662"},{"duration":"51","assistant_action":[],"orientation":"南","distance":"64","road":"东直门北大街","instruction":"沿东直门北大街向南步行64米","action":[],"walk_type":"0","polyline":"116.433556,39.941662;116.433578,39.94109"},{"duration":"593","assistant_action":[],"orientation":"南","distance":"741","road":"东直门南大街","instruction":"沿东直门南大街向南步行741米","action":[],"walk_type":"0","polyline":"116.433578,39.941086;116.433617,39.940491;116.433617,39.940491;116.433655,39.939613;116.433655,39.939613;116.433655,39.938828;116.433655,39.938828;116.433678,39.938187;116.433678,39.938187;116.433685,39.937706;116.433685,39.937706;116.433708,39.937584;116.433708,39.937462;116.433708,39.937462;116.433716,39.937164;116.433716,39.937164;116.433739,39.936714;116.433739,39.936714;116.433762,39.936329;116.433762,39.936329;116.433769,39.936134;116.433769,39.936134;116.433807,39.93568;116.433807,39.93568;116.433823,39.935169;116.43383,39.935146;116.43383,39.935146;116.433868,39.934357"},{"duration":"257","assistant_action":[],"orientation":"南","distance":"321","road":"东直门南大街","instruction":"沿东直门南大街向南步行321米直行","action":"直行","walk_type":"0","polyline":"116.433868,39.934345;116.433922,39.933125;116.433922,39.933125;116.433968,39.932228;116.433968,39.932228;116.433975,39.932083;116.433975,39.93177;116.433975,39.931675;116.433929,39.931454"},{"duration":"414","assistant_action":[],"orientation":"南","distance":"517","road":"朝阳门北大街","instruction":"沿朝阳门北大街向南步行517米直行","action":"直行","walk_type":"0","polyline":"116.433929,39.93145;116.433952,39.931107;116.433952,39.931107;116.433952,39.931061;116.433952,39.931061;116.433968,39.930843;116.433968,39.930843;116.433968,39.930782;116.433968,39.930782;116.433975,39.930668;116.433975,39.930668;116.433998,39.930225;116.433998,39.930225;116.434036,39.929554;116.434036,39.929554;116.434036,39.929497;116.434036,39.929497;116.434052,39.928902;116.434052,39.928902;116.434097,39.928295;116.434097,39.928295;116.434113,39.927544;116.434113,39.927544;116.434135,39.927319;116.434143,39.927063;116.434143,39.927063;116.434151,39.926994;116.434151,39.926994;116.434158,39.92675"},{"duration":"185","assistant_action":[],"orientation":[],"distance":"231","road":"朝阳门桥","instruction":"沿朝阳门桥步行231米左转","action":"左转","walk_type":"0","polyline":"116.434158,39.926746;116.434097,39.926529;116.434097,39.926529;116.434036,39.926224;116.434036,39.926037;116.434082,39.925613;116.434082,39.925613;116.434097,39.925064;116.43409,39.924755;116.43409,39.924755;116.434196,39.924755"},{"duration":"29","assistant_action":[],"orientation":[],"distance":"36","road":[],"instruction":"步行36米右转","action":"右转","walk_type":"20","polyline":"116.434196,39.924759;116.434196,39.925018;116.434273,39.925018"},{"duration":"236","assistant_action":[],"orientation":"南","distance":"295","road":"朝阳门北大街","instruction":"沿朝阳门北大街向南步行295米直行","action":"直行","walk_type":"0","polyline":"116.434288,39.925018;116.434311,39.924129;116.434326,39.923882;116.434326,39.923882;116.434357,39.923271;116.434364,39.922577;116.434364,39.922577;116.434372,39.922348"},{"duration":"586","assistant_action":[],"orientation":"南","distance":"732","road":"朝阳门南大街","instruction":"沿朝阳门南大街向南步行732米直行","action":"直行","walk_type":"0","polyline":"116.434372,39.92234;116.434364,39.922001;116.434364,39.922001;116.434364,39.921986;116.434364,39.921986;116.43438,39.921516;116.43438,39.921516;116.43438,39.92141;116.43438,39.92141;116.434402,39.921093;116.434402,39.921093;116.434418,39.920681;116.434418,39.920681;116.434425,39.920303;116.434425,39.920303;116.434471,39.919453;116.434471,39.919453;116.434509,39.918636;116.434509,39.918636;116.434532,39.917988;116.434532,39.917988;116.434532,39.917969;116.434563,39.917244;116.434563,39.917244;116.43457,39.916912;116.43457,39.916912;116.434593,39.916492;116.434593,39.916492;116.434601,39.916298;116.434601,39.916298;116.434631,39.915798;116.434631,39.915798;116.434631,39.915707"},{"duration":"655","assistant_action":[],"orientation":"南","distance":"819","road":"建国门北大街","instruction":"沿建国门北大街向南步行819米直行","action":"直行","walk_type":"0","polyline":"116.434631,39.915699;116.434639,39.915562;116.434639,39.915562;116.434647,39.915421;116.434647,39.915421;116.434654,39.915081;116.434654,39.915081;116.434654,39.914951;116.434654,39.914951;116.434692,39.91428;116.434692,39.91428;116.434738,39.913879;116.434738,39.913879;116.434753,39.913551;116.434753,39.913551;116.434761,39.91349;116.434761,39.91349;116.434784,39.913025;116.434784,39.913025;116.434807,39.91251;116.434814,39.912266;116.434814,39.912266;116.434868,39.911217;116.434868,39.911217;116.434906,39.91061;116.434914,39.91011;116.434914,39.91011;116.434959,39.909058;116.434959,39.909058;116.434959,39.909035;116.434952,39.908993;116.434937,39.908932;116.434891,39.908871;116.434891,39.908871;116.43483,39.908726;116.434814,39.908653;116.434814,39.908497;116.43483,39.908428;116.434868,39.90831"},{"duration":"22","assistant_action":[],"orientation":"东南","distance":"28","road":"建国门南大街","instruction":"沿建国门南大街向东南步行28米右转","action":"右转","walk_type":"0","polyline":"116.434868,39.908302;116.434975,39.908195;116.434998,39.908142;116.435005,39.908104;116.435005,39.908081"},{"duration":"38","assistant_action":"到达目的地","orientation":[],"distance":"48","road":[],"instruction":"步行48米到达目的地","action":[],"walk_type":"0","polyline":"116.435005,39.90807;116.434456,39.90807"}]}],"origin":"116.481028,39.989644","destination":"116.434446,39.90816"},"count":"1","infocode":"10000","status":"1","info":"ok"}ok


手机观看扫描下方微信小程序二维码


知识共享许可协议本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。

相关推荐

目录展开