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. dbutil组件的常见用法

    该工具包主要用来操作数据库,进行增删改查.将结果包装到对象或对象集合中. 在写web项目的时候,经常会涉及到数据库的操作.比如连接数据库获取连接对象.执行sql语句.获得结果.如果对每一个方法都写这么 ...

  2. Java数据结构和算法(二):数组

    上篇博客我们简单介绍了数据结构和算法的概念,对此模糊很正常,后面会慢慢通过具体的实例来介绍.本篇博客我们介绍数据结构的鼻祖——数组,可以说数组几乎能表示一切的数据结构,在每一门编程语言中,数组都是重要 ...

  3. linux 下面压缩,解压.rar文件以及rar,unrar实例

    http://www.rarlab.com/download.htm [root@bass src]# wget http://www.rarlab.com/rar/rarlinux-x64-5.4. ...

  4. ubutun:从共享文件夹拷贝文件尽量使用cp命令而不是CTRL+C/V

    为了方便,VBOX安装的Ubuntu,并在硬盘上创建了一个与Windows的共享文件夹sharefolder方便在两个系统之间传文件 但是经常发现的问题就是从sharefolder中拷贝文件到ubun ...

  5. js prototype 理解

    简单理解:prototype对象是实现面向对象的一个重要机制.每个函数也是一个对象,它们对应的类就是 function,每个函数对象都具有一个子对象prototype.Prototype 表示了该函数 ...

  6. Shape Control for .NET

    Shape Control for .NET Yang Kok Wah, 23 Mar 2017 CPOL    4.83 (155 votes)   Rate this: vote 1vote 2v ...

  7. man page用法

    通过man man可查看man page的具体用法. 1   Executable programs or shell commands       2   System calls (functio ...

  8. Sys未定义处理方法

    网上很多方法都试过,但是我发现,如果我把iis的端口号换一下,然后重新部署,就正常了,然后你可以再换回原来的端口号,也不会再报错了,应该是微软的一个bug,不知道大伙有什么更好的办法么

  9. ardunio

    fritzing,  arduino简易电路图制作软件

  10. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...