code show as below:
import java.util.HashMap;
import java.util.Map;
public class MapDistance {
private static double EARTH_RADIUS = 6378.137;
private static double rad(double d) {
return d * Math.PI / 180.0;
}
/**
* Calculate the distance between two places according to the latitude and longitude of the two locations (unit is KM)
* The parameter is of type String
* @param lat1 user longitude
* @param lng1 user latitude
* @param lat2 merchant longitude
* @param lng2 business latitude
* @return
*/
public static String getDistance(String lat1Str, String lng1Str, String lat2Str, String lng2Str) {
Double lat1 = Double.parseDouble(lat1Str);
Double lng1 = Double.parseDouble(lng1Str);
Double lat2 = Double.parseDouble(lat2Str);
Double lng2 = Double.parseDouble(lng2Str);
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double difference = radLat1 - radLat2;
double mdifference = rad(lng1) - rad(lng2);
double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(mdifference / 2), 2)));
distance = distance * EARTH_RADIUS;
distance = Math.round(distance * 10000) / 10000;
String distanceStr = distance+"";
distanceStr = distanceStr.
substring(0, distanceStr.indexOf("."));
return distanceStr;
}
/**
* Get the latitude and longitude value of the current user within a certain distance
* Unit meters return minLat
* Minimum longitude minLng
* Minimum latitude maxLat
* maximum longitude maxLng
* maximum latitude minLat
*/
public static Map getAround(String latStr, String lngStr, String raidus) {
Map map = new HashMap();
Double latitude = Double.parseDouble(latStr);// pass the value to the longitude
Double longitude = Double.parseDouble(lngStr);// pass the value to the latitude
Double degree = (24901 * 1609) / 360.0; // get every degree
double raidusMile = Double.parseDouble(raidus);
Double mpdLng = Double.parseDouble((degree * Math.cos(latitude * (Math.PI / 180))+"").replace("-", ""));
Double dpmLng = 1 / mpdLng;
Double radiusLng = dpmLng * raidusMile;
//get the minimum longitude
Double minLat = longitude - radiusLng;
// get the maximum longitude
Double maxLat = longitude + radiusLng;
Double dpmLat = 1 / degree;
Double radiusLat = dpmLat * raidusMile;
// get the minimum latitude
Double minLng = latitude - radiusLat;
// get the maximum latitude
Double maxLng = latitude + radiusLat;
map.put("minLat", minLat+"");
map.put("maxLat", maxLat+"");
map.put("minLng", minLng+"");
map.put("maxLng", maxLng+"");
return map;
}
public static void main(String[] args) {
//Test latitude and longitude: 117.11811 36.68484
//Test latitude and longitude 2: 117.00999000000002 36.66123
//System.out.println(getDistance("117.11811","36.68484","117.00999000000002","36.66123"));
System.out.println(getAround("117.11811", "36.68484", "13000"));
//117.01028712333508(Double), 117.22593287666493(Double),
//36.44829619896034(Double), 36.92138380103966(Double)
}
}
http://www.cnblogs.com/sprinng/p/4922999.html