第一步:在百度地图开放平台获取密钥
第二步导入jar包:主要是json的相关jar包
json-lib-2.4-jdk13.jar
commons-lang-2.6.jar
commons-beanutils-1.9.3.jar
commons-collections-3.2.1.jar
commons-logging-1.2.jar
ezmorph-1.0.6.jar
spring-core-4.3.9.RELEASE.jar
第三步:创建经纬度的类
package map;
public class LatitudeAndLongitude {
private double Longitude;//经度
private double Latitude;//纬度
public double getLongitude() {
return Longitude;
}
public void setLongitude(double lng) {
Longitude = lng;
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double lat) {
Latitude = lat;
}
@Override
public String toString() {
return "LatitudeAndLongitude [Longitude=" + Longitude + ", Latitude="
+ Latitude + "]";
}
}
第三步:通过密钥获取地点的经纬度,并且计算距离
package map;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.springframework.util.StringUtils;
import net.sf.json.JSONObject;
public class getLngAndLat {
/**
* 根据地址获得经纬度,把代码中的ak值更改为你自己的ak值
*/
public static LatitudeAndLongitude getLngAndLat(String address) {
LatitudeAndLongitude latAndLng = new LatitudeAndLongitude();
String url = "http://api.map.baidu.com/geocoding/v3/?address="+address+"&output=json&ak=你自己的ak值";//GET请求
String json = loadJSON(url);
System.out.println(json);
if (StringUtils.isEmpty(json))
{
return latAndLng;
}
int len = json.length();
// 如果不是合法的json格式
if (json.indexOf("{") != 0 || json.lastIndexOf("}") != len - 1) {
return latAndLng;
}
JSONObject obj = JSONObject.fromObject(json);
if (obj.get("status").toString().equals("0")) {
double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
System.out.println(lng+"1");
double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
latAndLng.setLatitude(lat);
latAndLng.setLongitude(lng);
}
return latAndLng;
}
public static String loadJSON(String url) {
StringBuilder json = new StringBuilder();
try {
URL urlObj = new URL(url);
URLConnection uc = urlObj.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine = null;
while ((inputLine = br.readLine()) != null) {
json.append(inputLine);
}
br.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return json.toString();
}
public static void main(String[] args) {
LatitudeAndLongitude latAndLng = getLngAndLat.getLngAndLat("广东省广州市");
LatitudeAndLongitude latAndLng2 = getLngAndLat.getLngAndLat("广西省南宁市");
double distance=getLngAndLat.getDistance
(latAndLng.getLongitude(), latAndLng.getLatitude(), latAndLng2.getLongitude(), latAndLng2.getLatitude());
System.out.println(distance);
System.out.println("经度:" + latAndLng.getLongitude() + "---纬度:" + latAndLng.getLatitude());
System.out.println("经度:" + latAndLng2.getLongitude() + "---纬度:" + latAndLng2.getLatitude());
}
/**
* 计算两点之间真实距离
* @return 千米
*/
public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
// 维度
double lat1 = (Math.PI / 180) * latitude1;
double lat2 = (Math.PI / 180) * latitude2;
// 经度
double lon1 = (Math.PI / 180) * longitude1;
double lon2 = (Math.PI / 180) * longitude2;
// 地球半径
double R = 6371;
// 两点间距离 km,如果想要米的话,结果*1000就可以了
double d = Math.acos
(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
return d ;
}
}