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. MFC显示GIF动画图片

    本帖则将讨论如何在MFC的对话框里显示GIF动画图片.一些关于传统控件的美化方法正在研究当中会陆续发帖的. 这是本帖用到的一个VS2008例程.  附件  GifPicture.rar (138.1 ...

  2. C++多态有哪几种方式?

    C++多态方式: (1)静态多态(重载,模板) 是在编译的时候,就确定调用函数的类型. (2)动态多态(覆盖,虚函数实现) 在运行的时候,才确定调用的是哪个函数,动态绑定.运行基类指针指向派生类的对象 ...

  3. nodejs http代理请求

    一些免费到代理地址 http://www.xicidaili.com/nn https://proxy.l337.tech/txt http://www.66ip.cn/nm.html 以下代码可以测 ...

  4. 删除mysql数据库表里的记录

    如果记录不再需要,可以用delete 命令进行删除,语法如下: DELETE FROM tablename [WHERE CONDITION] 例如,在emp 中将ename 为‘dony’的记录全部 ...

  5. 阿里云maven 库

    阿里云maven 库 , 好用,速度快 maven setting.xml https://github.com/ae6623/Zebra/blob/master/maven-repo-setting ...

  6. EditText禁止输空格

    1.EditText禁止输空格 editText.setFilters(new InputFilter[]{filter}); private InputFilter filter=new Input ...

  7. CSS3多背景应用

    /*多背景应用*/ .wrapper { width: 640px; height: 1000px; margin: auto; background: url(./images/head.jpg) ...

  8. VBOX Ubuntu设置与Windows的共享文件夹

    参考资料: http://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html http://zycao.com/virtualbox-ubu ...

  9. 解决在eclipse中配置Tomcat时,出现"Cannot create a server using the selected type"的错误

    比如说使用tomcat 这是因为你之前创建过一次,比如说tomcat6,你指定的目录是:D:/tomcat-6.0.3 后来因为某种原因你把tomcat删了,然后你又安装到了E:/tomcat-6.0 ...

  10. 部署静态页面到nginx

    1.将页面上传到某个位置.比如:/home/myapp/navi 2. server_name localhost;  localhost 更换为自己的域名 3.更改配置文件./usr/local/n ...