import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
* 公共路线接口方法,计算实际距离。
*/
public class MapDistanceUtil {

private static final String HOSTARR = "http://restapi.amap.com/v3/geocode/regeo";
private static final String HOST = "https://restapi.amap.com/v3/distance";
// private static final String HOST = "http://122.114.79.163:7770/v3/distance";
private static final String KEY = "761e73b6188fb868feee5b558dcbcd7a";

/**
* 参数:(出发点数组、目的点) 格式:经度,纬度
* 返回: {
* "status": "1",
* "info": "OK",
* "infocode": "10000",
* "results": [{ //结果数组
* "origin_id": "1", //第一个起点
* "dest_id": "1", //目的地
* "distance": "261278", //距离(米)
* "duration": "14280" //预计时间(秒)
* }]
* }
*/
public static String mapDistanceMethod(String[] origins, String destination) {
String responseEntity = null;
for (int i = 0; i < 5 && responseEntity == null; i++) {
responseEntity = mapDistance(origins, destination);
}
return responseEntity;
}

public static String getLocationAddr(String location){
List<NameValuePair> list = new LinkedList<>();
list.add(new BasicNameValuePair("key", KEY));
list.add(new BasicNameValuePair("output", "JSON"));
list.add(new BasicNameValuePair("location", location));
String responseEntity = null;
String addr="";
try {
HttpGet httpGet = new HttpGet(new URIBuilder(HOSTARR).setParameters(list).build());
httpGet.setConfig(RequestConfig.custom().setConnectTimeout(2000).build());
HttpResponse response = HttpClients.createDefault().execute(httpGet);
responseEntity = EntityUtils.toString(response.getEntity(), "UTF-8");
JSONObject jsonObject=JSON.parseObject(responseEntity);
addr=jsonObject.getJSONObject("regeocode").getJSONObject("addressComponent")
.getJSONObject("building").getString("name");
if(addr.equals("[]")){
addr=jsonObject.getJSONObject("regeocode").getString("formatted_address");
if(addr.indexOf("省")>0){
addr=addr.substring(addr.indexOf("省")+1,addr.length());
}
if(addr.indexOf("市")>0){
addr=addr.substring(addr.indexOf("市")+1,addr.length());
}
}
//System.out.println("-------响应结果-------\n" + addr);
} catch (Exception e) {
if (e instanceof ConnectTimeoutException) {
System.out.println("-------请求超时-------");
} else {
e.printStackTrace();
}
}
return addr;
}

private static String mapDistance(String[] origins, String destination) {
StringBuilder originBuilder = new StringBuilder();
for (int i = 0; i < origins.length; i++) {
originBuilder.append(origins[i]);
if (i < origins.length - 1) {
originBuilder.append("|");
}
}
List<NameValuePair> list = new LinkedList<>();
list.add(new BasicNameValuePair("key", KEY));
list.add(new BasicNameValuePair("output", "JSON"));
list.add(new BasicNameValuePair("origins", originBuilder.toString()));
list.add(new BasicNameValuePair("destination", destination));
String responseEntity = null;
try {
HttpGet httpGet = new HttpGet(new URIBuilder(HOST).setParameters(list).build());
httpGet.setConfig(RequestConfig.custom().setConnectTimeout(2000).build());
HttpResponse response = HttpClients.createDefault().execute(httpGet);
responseEntity = EntityUtils.toString(response.getEntity(), "UTF-8");
//System.out.println("-------距离测量响应结果-------\n" + responseEntity);
} catch (Exception e) {
if (e instanceof ConnectTimeoutException) {
System.out.println("-------请求超时-------");
} else {
e.printStackTrace();
}
}
return responseEntity;
}

public static void main(String[] args) throws InterruptedException {
//getLocationAddr("113.242439,35.1863620");
System.out.println(getLocationAddr("113.242439,35.1863620"));
// String[] origins = new String[]{"116.481028,39.989643", "114.481028,39.989643", "115.481028,39.989643"};
// String destination = "114.465302,40.004717";
// for (int i = 0; i < 100; i++) {
// MapDistanceUtil.mapDistanceMethod(origins, destination);
// System.out.println("---------第" + i + "次请求");
// Thread.sleep(200);
// }
}
}

java后台高德经纬度转地理位置信息的更多相关文章

  1. 百度api:根据经纬度获取地理位置信息

    调用百度api,根据经度和纬度获取地理位置信息,返回Json. C#代码: using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Syste ...

  2. java根据GPS(经纬度)获取地理位置

    package cn.antiy.weiqing.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONAr ...

  3. Android 获取地理位置信息 封装好了 直接用

    前言:花了一个早上研究了以下android获取经纬度,然后网上的参考资料都是杂七杂八,基本上都是过去几年的,现在我用 android6.0参照别人的结果发生好多错误,我的内心几乎是崩溃的.后来,不断百 ...

  4. 项目源码--JAVA基于LBS地理位置信息应用的服务端

    技术要点: 1. LBS应用框架服务端实现 2. JAVA服务端技术 3. MYSQL数据库技术 4. 源码带详细的中文注释 ......   详细介绍: 1. LBS应用框架服务端实现 此套源码是基 ...

  5. java后台获取和js拼接展示信息

    java后台获取和js拼接展示信息: html页面代码: <div class="results-bd"> <table id="activityInf ...

  6. Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息

    本文主要介绍了Android项目集成百度地图API,使用AlarmManager定时调用Service,在Service中请求坐标更新,并通过坐标得到省.市和县三级地理位置信息的方法. 程序结构很简单 ...

  7. [微信开发] 微信JSAPI - 获取用户地理位置信息

    参考博客 http://blog.csdn.net/u013142781/article/details/50503299 主要JS 方法 wx.getLocation 获取地理位置信息传递参数 成功 ...

  8. 使用LocationManager来获取移动设备所在的地理位置信息

    在Android应用程序中,可以使用LocationManager来获取移动设备所在的地理位置信息.看如下实例:新建android应用程序TestLocation. 1.activity_main.x ...

  9. [转帖]挖洞经验 | 获取Facebook Marketplace卖家精确地理位置信息

    挖洞经验 | 获取Facebook Marketplace卖家精确地理位置信息 https://www.freebuf.com/vuls/202820.html 知识就是力量 5000刀的一个漏洞. ...

随机推荐

  1. Linux命令-文件处理命令:cat

    cat /etc/issue 查看etc目录下面的issue文件内容(issue是linxu系统的欢迎文件) cat -n /etc/issue 查看文件内容的时候显示行号 tac /etc/issu ...

  2. USES_CONVERSION的使用和注意

    USES_CONVERSION是用来转换类型的,比如我们很常见的问题: 在Socket编程时候,我们的IP地址从界面上输进去一般都使用CString类型的,可是在SOCKADDR_IN中的inet_a ...

  3. C++二维数组讲解、二维数组的声明和初始化

    我们知道,一维空间是一条线,数学中用一条数轴来表达:二维空间是一个平面,数学中用平面坐标系来表达.那么二维数组又是什么样的呢? 线与面 我们用一个下标来描述一维数组中的某个元素,就好像在用数描述一条线 ...

  4. C# AutoResetEvent

    文章转载自:C# AutoResetEvent AutoResetEvent 常常被用来在两个线程之间进行信号发送 AutoResetEvent是.net线程简易同步方法中的一种,两个线程共享相同的A ...

  5. appium 1.6.3 + ios 10.2 + xcode 8.2.1 真机运行iphone app

    appium命令行启动: appium --address "127.0.0.1" --session-override --pre-launch --debug-log-spac ...

  6. Step by Step Learn Python(1)

    print "Hello World!" action = raw_input("please select your action{1, 2, 3, 4, 5, 6, ...

  7. XVAG音频文件格式提取

    索尼的一种游戏音频格式,通过vgmstream工具可以提取 工具已经下载好:(包含dll包) http://files.cnblogs.com/files/hont/vgmstream-r1040-t ...

  8. 05、Windows Store app 的图片裁切(更新)

    在 Win Phone Silverlight api 中,有一个 PhotoChooserTask 选择器,指定宽.高属性,在选择图片的时候, 可以进行裁切,代码: PhotoChooserTask ...

  9. DLL编写中extern “C”和__stdcall的作用

    动态链接库的使用有两种方式,一种是显式调用.一种是隐式调用. (1)       显式调用:使用LoadLibrary载入动态链接库.使用GetProcAddress获取某函数地址. (2)      ...

  10. C#使用SendMessage传递字符串

    来源:http://www.cnblogs.com/sizzle/archive/2007/08/29/874796.html 正文: 在C#中使用SendMessage,原本以为很简单的事,却处处碰 ...