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. iis 设置了主机名 就不能访问

    主机名就是域名,设置这个主要用来防止别人通过IP访问,对于服务器来说多少会更安全点,不过如果没有域名,则设置后无法访问   追答 如果想要测试域名,则可以修改hosts文件实现,这样就可以设置主机名, ...

  2. ubuntu下载软件安装包

    apt-get -d download xxx ubuntu下载软件安装包命令.仅仅下载deb格式的安装包,不安装. xxx是待下载的安装包.

  3. 有道翻译 / 百度翻译Api

    比较推荐使用百度翻译api 不推荐有道翻译,比较水. http://ai.youdao.com/docs/doc-trans-api.s#p02 http://ai.youdao.com/docs/d ...

  4. pip install mysql-connector 安装出错

    一.MySQL Connector/Python 2.2.3 的变化: 之前 mysql 官方说MySQL Connector/Python 是纯python语言写的,但是呢! 这个问题在2.2.3中 ...

  5. oracle 快速批量插入复杂数据的内容

    最近迷上一种批量插入的方法,一句sql解决,将需要插入的数据用with as 的方式查出来,不管多么复杂的sql,都可以用临时表的方式查出来,然后直接插入,这样代码更加清晰 流程也简单 insert ...

  6. 解决:ubuntu 里文件夹带锁

    sudo chown -R <user-name> <folder-name> /* 其中-R的意思是recursive,你懂的,chown --help可以查看帮助信息 */ ...

  7. hdu 5384 Danganronpa(字典树)

    题意: f(A,B)表示:B在A中作为子串出现的次数. 题目给出n个证据,m个子弹 Ai是证据.Bi是子弹.题目问:全部Bi对每一个Ai造成的伤害是多少,即每一个Bi在Ai中出现的次数总和. 解析: ...

  8. jQuery插件开发全解析<转>

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  9. mf210v 端口的映射

    ttyUSB0 : 诊断端口 ttyUSB1 : AT指令端口 ttyUSB2 : VoUSB端口(语音) ttyUSB3 : Modem端口

  10. Unix网络编程中的五种I/O模型_转

    转自:Unix网络编程中的的五种I/O模型 下面主要是把unp第六章介绍的五种I/O模型. 1. 阻塞I/O模型 例如UDP函数recvfrom的内核到应用层.应用层到内核的调用过程是这样的:首先把描 ...