zoukankan      html  css  js  c++  java
  • Java调用百度地图API

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

      1.确定输入地址的坐标

      2.两个坐标的距离

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

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

    示例代码:

    import com.alibaba.fastjson.JSON;
    import com.google.common.collect.ImmutableMap;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.http.client.fluent.Request;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Service;
    
    import java.io.IOException;
    import java.util.Map;
    
    /**
     * 百度地图api接口调用
     */
    @Service("geocodingService")
    @Transactional
    public class GeocodingService {
    
        private static final Logger LOG = LoggerFactory.getLogger(GeocodingService.class);
    
        private static final Double PI = Math.PI;
    
        private static final Double PK = 180 / PI;
    
        private static final String MAP_URL= "http://api.map.baidu.com/geocoder/v2/?ak=4rcKAZKG9OIl0wDkICSLx8BA&output=json&address=";
    
        /**
         * 根据地址获取经纬度
         * @param address
         * @return
         */
        private Map<String,Double> getLatAndLngByAddress(String address){
            Map<String,Double> poiMap = null;
            String result = null;
            try {
                result = Request.Get(MAP_URL+ address)
                        .connectTimeout(1000).socketTimeout(1000)
                        .execute().returnContent().asString();
            } catch (IOException e) {
                LOG.error("调用百度地图API获取{}的经纬度,抛错{}",address,e);
            }
            if (StringUtils.isNotBlank(result) && "0".equals(JSON.parseObject(result).get("status") + "")){
                String lat = result.substring(result.indexOf(""lat":")
                        + (""lat":").length(), result.indexOf("},"precise""));
                String lng = result.substring(result.indexOf(""lng":")
                        + (""lng":").length(), result.indexOf(","lat""));
                poiMap = ImmutableMap.of("lat",Double.parseDouble(lat),"lng",Double.parseDouble(lng));
            }
            return poiMap;
        }
    
        /**
         * 计算两个地址的距离(米)
         * @param address
         * @param otherAddress
         * @return
         */
        public Double getDistanceFromTwoPlaces(String address,String otherAddress){
            Double distance = null;
            if (StringUtils.isNotBlank(address) && StringUtils.isNotBlank(otherAddress)){
                address = address.trim();
                otherAddress = otherAddress.trim();
                if (address.equals(otherAddress)){
                    return 0.0d;
                }
                Map pointA = getLatAndLngByAddress(address);
                Map pointB = getLatAndLngByAddress(otherAddress);
                distance = getDistanceFromTwoPoints(pointA,pointB);
            }
            return distance;
        }
    
        /**
         * 获取两个经纬度之间的距离(单位:米)
         * @param pointA
         * @param pointB
         * @return
         */
        private Double getDistanceFromTwoPoints(Map pointA, Map pointB) {
            Double distance = null;
            if (pointA != null && !pointA.isEmpty() && pointB != null && !pointB.isEmpty()){
                double lat_a = (double) pointA.get("lat");
                double lng_a = (double) pointA.get("lng");
                double lat_b = (double) pointB.get("lat");
                double lng_b = (double) pointB.get("lng");
    
                if (lat_a == lat_b && lng_a == lng_b){
                    return 0.0d;
                }
    
                double t1 = Math.cos(lat_a / PK) * Math.cos(lng_a / PK) * Math.cos(lat_b / PK) * Math.cos(lng_b / PK);
                double t2 = Math.cos(lat_a / PK) * Math.sin(lng_a / PK) * Math.cos(lat_b / PK) * Math.sin(lng_b / PK);
                double t3 = Math.sin(lat_a / PK) * Math.sin(lat_b / PK);
    
                double tt = Math.acos(t1 + t2 + t3);
                distance = 6366000 * tt;
            }
            return distance;
        }
    }

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

  • 相关阅读:
    OZ Report 오즈 리포트 개발
    日期 英文 英语 韩文 韩语
    行合并
    R内存扩展 win7内存扩展
    计算日期函数
    C#颜色 Color.FromArgb ColorTranslator 16进制
    Phone
    浏览器 UserAgent
    context.Request.Files post 上传问题件
    The connection to adb is down, and a severe error has occured.
  • 原文地址:https://www.cnblogs.com/itommy/p/10610468.html
Copyright © 2011-2022 走看看