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

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

java调用高德地图api获取经纬度的方法,废话少说,直接上代码:

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.sun.deploy.net.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 = "http://restapi.amap.com/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();

            }

        }

    }

}




高德官网api详情:http://lbs.amap.com/api/webservice/reference/georegeo/


  • 发表于 2017-11-24 15:08
  • 阅读 ( 3110 )
  • 分类:Java

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
不写代码的码农
江南烟雨

可以跨境界的java开发攻城狮

36 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章