Java调用百度地图API

本实战代码将使用百度地图的接口来实现以下功能:

  1.确定输入地址的坐标

  2.两个坐标的距离

其他的话,还要使用百度账户申请相关的api,具体见:

http://lbsyun.baidu.com/index.php?title=webapi

示例代码:

1import com.alibaba.fastjson.JSON; 2import com.google.common.collect.ImmutableMap; 3import org.apache.commons.lang3.StringUtils; 4import org.apache.http.client.fluent.Request; 5import org.slf4j.Logger; 6import org.slf4j.LoggerFactory; 7import org.springframework.stereotype.Service; 8 9import java.io.IOException; 10import java.util.Map; 11 12/** 13 * 百度地图api接口调用 14 */ 15@Service("geocodingService") 16@Transactional 17public class GeocodingService { 18 19 private static final Logger LOG = LoggerFactory.getLogger(GeocodingService.class); 20 21 private static final Double PI = Math.PI; 22 23 private static final Double PK = 180 / PI; 24 25 private static final String MAP_URL= "http://api.map.baidu.com/geocoder/v2/?ak=4rcKAZKG9OIl0wDkICSLx8BA&output=json&address="; 26 27 /** 28 * 根据地址获取经纬度 29 * @param address 30 * @return 31 */ 32 private Map<String,Double> getLatAndLngByAddress(String address){ 33 Map<String,Double> poiMap = null; 34 String result = null; 35 try { 36 result = Request.Get(MAP_URL+ address) 37 .connectTimeout(1000).socketTimeout(1000) 38 .execute().returnContent().asString(); 39 } catch (IOException e) { 40 LOG.error("调用百度地图API获取{}的经纬度,抛错{}",address,e); 41 } 42 if (StringUtils.isNotBlank(result) && "0".equals(JSON.parseObject(result).get("status") + "")){ 43 String lat = result.substring(result.indexOf("\"lat\":") 44 + ("\"lat\":").length(), result.indexOf("},\"precise\"")); 45 String lng = result.substring(result.indexOf("\"lng\":") 46 + ("\"lng\":").length(), result.indexOf(",\"lat\"")); 47 poiMap = ImmutableMap.of("lat",Double.parseDouble(lat),"lng",Double.parseDouble(lng)); 48 } 49 return poiMap; 50 } 51 52 /** 53 * 计算两个地址的距离(米) 54 * @param address 55 * @param otherAddress 56 * @return 57 */ 58 public Double getDistanceFromTwoPlaces(String address,String otherAddress){ 59 Double distance = null; 60 if (StringUtils.isNotBlank(address) && StringUtils.isNotBlank(otherAddress)){ 61 address = address.trim(); 62 otherAddress = otherAddress.trim(); 63 if (address.equals(otherAddress)){ 64 return 0.0d; 65 } 66 Map pointA = getLatAndLngByAddress(address); 67 Map pointB = getLatAndLngByAddress(otherAddress); 68 distance = getDistanceFromTwoPoints(pointA,pointB); 69 } 70 return distance; 71 } 72 73 /** 74 * 获取两个经纬度之间的距离(单位:米) 75 * @param pointA 76 * @param pointB 77 * @return 78 */ 79 private Double getDistanceFromTwoPoints(Map pointA, Map pointB) { 80 Double distance = null; 81 if (pointA != null && !pointA.isEmpty() && pointB != null && !pointB.isEmpty()){ 82 double lat_a = (double) pointA.get("lat"); 83 double lng_a = (double) pointA.get("lng"); 84 double lat_b = (double) pointB.get("lat"); 85 double lng_b = (double) pointB.get("lng"); 86 87 if (lat_a == lat_b && lng_a == lng_b){ 88 return 0.0d; 89 } 90 91 double t1 = Math.cos(lat_a / PK) * Math.cos(lng_a / PK) * Math.cos(lat_b / PK) * Math.cos(lng_b / PK); 92 double t2 = Math.cos(lat_a / PK) * Math.sin(lng_a / PK) * Math.cos(lat_b / PK) * Math.sin(lng_b / PK); 93 double t3 = Math.sin(lat_a / PK) * Math.sin(lat_b / PK); 94 95 double tt = Math.acos(t1 + t2 + t3); 96 distance = 6366000 * tt; 97 } 98 return distance; 99 } 100}

http://blog.csdn.net/u013142781/article/details/47085369

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )