import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class AmapUtil {
private final static String key = "8091c6a781f6af3e59b599dd3317d28d";
private final static String baseUrl = "http://restapi.amap.com/v3/";
public static BigDecimal getDistanceByGaoDe(BigDecimal originlon, BigDecimal originlat, BigDecimal deslon,
BigDecimal deslat) {
String origins = originlon + "," + originlat;
String destination = deslon + "," + deslat;
int strategy = 2;
BigDecimal distance = null;
String url = baseUrl + "direction/driving?" + "origin=" + origins + "&destination=" + destination + "&strategy="
+ strategy + "&extensions=base&key=" + key;
JSONObject jsonobject = JSONObject.fromObject(getHttpResponse(url));
if ("1".equals(jsonobject.getString("status")) && StringUtils.equalsIgnoreCase("ok", jsonobject.getString("info"))) {
JSONArray pathArray = jsonobject.getJSONObject("route").getJSONArray("paths");
String distanceString = pathArray.getJSONObject(0).getString("distance");
distance = new BigDecimal(distanceString);
}
return distance;
}
public static BigDecimal getDistanceByGeometry(double startlongitude, double startdimensionality,
double endlongitude, double enddimensionality) {
double lon1 = (Math.PI / 180) * startlongitude;
double lon2 = (Math.PI / 180) * endlongitude;
double lat1 = (Math.PI / 180) * startdimensionality;
double lat2 = (Math.PI / 180) * enddimensionality;
double R = 6371;
double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1))
* R;
BigDecimal distance = new BigDecimal(d * 1000);
return distance;
}
public static String coordinate(String address) {
try {
address = URLEncoder.encode(address,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String coordinateString = "";
String url = baseUrl + "geocode/geo?address=" + address + "&output=json&key=" + key;
JSONObject jsonobject = JSONObject.fromObject(getHttpResponse(url));
if ("1".equals(jsonobject.getString("status")) && StringUtils.equalsIgnoreCase("ok", jsonobject.getString("info"))) {
JSONArray pathArray = jsonobject.getJSONArray("geocodes");
coordinateString = pathArray.getJSONObject(0).getString("location");
}
return coordinateString;
}
public static String convert(BigDecimal lon, BigDecimal lat) {
String coordsys = lon + "," + lat;
try {
coordsys = URLEncoder.encode(coordsys, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String coordinateString = "";
String url = baseUrl + "assistant/coordinate/convert?locations=" + coordsys + "&coordsys=gps&output=json&key="
+ key;
JSONObject jsonobject = JSONObject.fromObject(getHttpResponse(url));
if ("1".equals(jsonobject.getString("status")) && StringUtils.equalsIgnoreCase("ok", jsonobject.getString("info"))) {
coordinateString = jsonobject.getString("locations");
}
return coordinateString;
}
public static String getAddress(BigDecimal lon, BigDecimal lat) {
String location = lon + "," + lat;
String url = baseUrl + "geocode/regeo?key=" + key + "&location=" + location;
try {
location = URLEncoder.encode(location, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonobject = JSONObject.fromObject(getHttpResponse(url));
String addressString = "";
if ("1".equals(jsonobject.getString("status")) && StringUtils.equalsIgnoreCase("ok", jsonobject.getString("info"))) {
String regeocodeString = jsonobject.getString("regeocode");
JSONObject regeocodeJson = JSONObject.fromObject(regeocodeString);
addressString = regeocodeJson.getString("formatted_address");
}
return addressString;
}
public static String getLocalCity(BigDecimal lon, BigDecimal lat) {
String location = lon + "," + lat;
String url = baseUrl + "geocode/regeo?key=" + key + "&location=" + location + "&radius=3000&roadlevel=1";
String city = "";
try {
location = URLEncoder.encode(location, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
JSONObject jsonobject = JSONObject.fromObject(getHttpResponse(url));
if ("1".equals(jsonobject.getString("status")) && StringUtils.equalsIgnoreCase("ok", jsonobject.getString("info"))) {
String regeocodeString = jsonobject.getString("regeocode");
JSONObject regeocodeJson = JSONObject.fromObject(regeocodeString);
String addressString = regeocodeJson.getString("addressComponent");
JSONObject cityaddress = JSONObject.fromObject(addressString);
city = cityaddress.getString("citycode");
}
return city;
}
public static String getHttpResponse(String allConfigUrl) {
BufferedReader in = null;
StringBuffer result = null;
try {
URI uri = new URI(allConfigUrl);
URL url = uri.toURL();
URLConnection connection = url.openConnection();
connection.setRequestProperty("Content-type", "text/html");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("contentType", "utf-8");
connection.connect();
result = new StringBuffer();
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result.toString();
}
public static void main(String[] args) {
String address = "南京市秦淮区中华路夫子庙";
String coordinate = coordinate(address);
System.out.println(address+"地址转换高德坐标-----" + coordinate);
}
}
pom.xml加入依赖
<!--阿里高德地图-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.4.0</version>
</dependency>