java 地图api接口_Java调用百度地图API
本實戰(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四色定理的简单证明
- 下一篇: Retrofit+OKHttp+RxJa