700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java api从高德地图获取某个位置的经纬度

java api从高德地图获取某个位置的经纬度

时间:2020-10-08 20:10:21

相关推荐

java api从高德地图获取某个位置的经纬度

1、代码展示

import com.fasterxml.jackson.databind.JsonNode;import com.ning.http.client.AsyncHttpClient;import com.ning.http.client.AsyncHttpClientConfig;import com.ning.http.client.ListenableFuture;import com.ning.http.client.Response;import com..HttpUtils;import org.mortbay.util.UrlEncoded;/*** 通过java api从高德地图获取经纬度* address 位置* output 返回结果格式* key 高德key值,需申请*/public class Distance {public static void main(String[] args) {//1、通过java api从高德地图获取经纬度String url = "/v3/geocode/geo?address=上海市东方明珠&output=JSON&key=xxxxxxxxx";AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();builder.setCompressionEnabled(true).setAllowPoolingConnection(true);builder.setRequestTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));builder.setIdleConnectionTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));AsyncHttpClient client = new AsyncHttpClient(builder.build());try {ListenableFuture<Response> future = client.prepareGet(url).execute();String result = future.get().getResponseBody();System.out.println(result);JsonNode jsonNode = new com.fasterxml.jackson.databind.ObjectMapper().readTree(future.get().getResponseBody());if(jsonNode.findValue("status").textValue().equals("1")) {JsonNode listSource = jsonNode.findValue("location");System.out.println(listSource);for(String location : listSource.textValue().split(",")){//得到这个位置的经纬度System.out.println(location);//System.out.println(Double.valueOf(location));}}} catch (Exception e) {e.printStackTrace();} finally {if(client != null){client.close();}}}}

2、详细请看高德官网api介绍:

/api/webservice/reference/georegeo/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。