日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 地图api接口_Java调用百度地图API

發(fā)布時間:2023/12/20 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 地图api接口_Java调用百度地图API 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本實戰(zhàn)代碼將使用百度地圖的接口來實現(xiàn)以下功能:

1.確定輸入地址的坐標

2.兩個坐標的距離

其他的話,還要使用百度賬戶申請相關(guān)的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接口調(diào)用

*/

@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=";

/**

* 根據(jù)地址獲取經(jīng)緯度

* @param address

* @return

*/

private Map getLatAndLngByAddress(String address){

Map poiMap = null;

String result = null;

try {

result = Request.Get(MAP_URL+ address)

.connectTimeout(1000).socketTimeout(1000)

.execute().returnContent().asString();

} catch (IOException e) {

LOG.error("調(diào)用百度地圖API獲取{}的經(jīng)緯度,拋錯{}",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;

}

/**

* 獲取兩個經(jīng)緯度之間的距離(單位:米)

* @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

總結(jié)

以上是生活随笔為你收集整理的java 地图api接口_Java调用百度地图API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。