本实战代码将使用百度地图的接口来实现以下功能:
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}