/**
* 假设地球为一半径为R的表面光滑圆球体,
* 表面上同一经线圈上相差1"两点间的距离为 2πR/360/3600
* 表面上同一纬线圈上相差1"两点间的距离为 2πR×cos(纬度)/360/3600
* 当R取半径平均值6371km时,
* 地球表面上同一经线圈上相差1"两点间的距离约为30.887m
* 地球表面上同一纬线圈上相差1"两点间的距离约为30.887m×cos(纬度)
*
* @param center
* @param radius
*/
private List<LatLng> calculateLl(LatLng center, double radius) {
List<LatLng> mList = new ArrayList<>();
double latitude = center.latitude;//维度
double longitude = center.longitude;//经度
//地球周长
Double perimeter = 2 * Math.PI * 6371000;
//纬度latitude的地球周长:latitude
Double perimeter_latitude = perimeter * Math.cos(Math.PI * latitude / 180);
//一米对应的经度(东西方向)1M实际度
double longitude_per_mi = 360 / perimeter_latitude;
double latitude_per_mi = 360 / perimeter;
Logger.debug(TAG, "经度(东西方向)1M实际度 ==" + longitude_per_mi);
Logger.debug(TAG, "纬度(南北方向)1M实际度 ==" + latitude_per_mi);
Double leftLo = longitude - (radius * longitude_per_mi);
Double rightLo = longitude + (radius * longitude_per_mi);
Double topLa = latitude + (radius * latitude_per_mi);
Double bottomLa = latitude - (radius * latitude_per_mi);
LatLng left = new LatLng(latitude, leftLo);
LatLng right = new LatLng(latitude, rightLo);
LatLng top = new LatLng(topLa, longitude);
LatLng bottom = new LatLng(bottomLa, longitude);
Logger.debug(TAG, "left ==" + left.toString());
Logger.debug(TAG, "right ==" + right.toString());
Logger.debug(TAG, "top ==" + top.toString());
Logger.debug(TAG, "bottom ==" + bottom.toString());
mList.add(left);
mList.add(right);
mList.add(top);
mList.add(bottom);
return mList;
}
计算Circle和Polygon是否全部包含
/**
* 圆是否包含在圆内
* 正反计算第一个圆到第二个圆心距离 加上 第一个圆半径是否大于第二个圆
*
* @return
*/
public boolean CircleContainsCircle( LatLng ll1 , double radius1 , LatLng ll2 , double radius2 ){
double dis = getPointDistace( ll1 , ll2 ) ;
if ( (dis + radius1) < radius2){
return true ;
}
return false ;
}
/**
* 圆是否包含在矩形内
* 1 ,判断圆心在矩形内
* 2 ,计算圆形到所有个点距离是否大于半径,
* 3 ,在计算圆心到每条边距离大于半径
* @return
*/
public boolean CircleContainsPolygon(LatLng ll1 , double radius1 , List<LatLng> listLL ){
Polygon mPolygon = addMapPolygon( mAMap , listLL ) ;
if ( !mPolygon.contains(ll1)){
// 如果圆形在方块外,必定不包含
return false ;
}
for (int i = 0; i < listLL.size() ; i++) {
double dis = getPointDistace( ll1 , listLL.get(i) ) ;
if (dis < radius1){
// 如果有点到圆心小于半径的就必定不包含
return false ;
}
}
// 将所有点计算成屏幕坐标计算
List<LatLng> mListLL = calculateLl(ll1 , radius1 ) ;
// 中心点屏幕坐标
Point mPoint = toScreenLocation( mAMap , ll1 ) ;
// 圆周某一个点屏幕尺寸
Point mPoint1 = toScreenLocation( mAMap , mListLL.get(0) ) ;
// 中心圆的屏幕尺寸
double radius2 = lineSpace(mPoint.x , mPoint.y , mPoint1.x , mPoint1.y ) ;
for (int i = 0; i < listLL.size() ; i++) {
Point mPointP = null ;
Point mPointN = null ;
if (i == listLL.size() -1){
mPointP = toScreenLocation(mAMap, listLL.get(i));
mPointN = toScreenLocation(mAMap, listLL.get(0));
}else {
mPointP = toScreenLocation(mAMap, listLL.get(i));
mPointN = toScreenLocation(mAMap, listLL.get(i + 1));
}
double d1 = pointToLine(mPointP.x , mPointP.y , mPointN.x , mPointN.y , mPoint.x , mPoint.y ) ;
if (d1 < radius2 ){
return false ;
}
}
return true ;
}
/**
* 矩形是否包含在圆内
* 计算五个点是否在圆内
* @return
*/
public boolean PolygonContainsCircle( List<LatLng> listLL1 , LatLng ll1 , double radius1 ){
Circle mCircle = addMapCircle( mAMap , ll1 , radius1 );
for (int i = 0; i < listLL1.size() ; i++) {
if ( !mCircle.contains(listLL1.get(i))){
return false ;
}
}
return true ;
}
/**
* 矩形是否包含在矩形内
* 比较所有点是否相同 , 并且最后一个点是否在前一个方内
* @return
*/
public boolean PolygonContainsPolygon(List<LatLng> listLL1 , List<LatLng> listLL2){
Polygon mPolygon = addMapPolygon( mAMap , listLL2 ) ;
for (int i = 0 ; i < listLL1.size() ; i++) {
if ( !mPolygon.contains(listLL1.get(i))){
return false ;
}
}
return true ;
}
计算点到线的最短距离
/**
* 计算点到线距离
* @param x1
* @param y1
* @param x2
* @param y2
* @param x0
* @param y0
* @return
*/
private double pointToLine(int x1, int y1, int x2, int y2, int x0, int y0) {
double space = 0;
double a, b, c;
a = lineSpace(x1, y1, x2, y2);// 线段的长度
b = lineSpace(x1, y1, x0, y0);// (x1,y1)到点的距离
c = lineSpace(x2, y2, x0, y0);// (x2,y2)到点的距离
if (c <= 0.000001 || b <= 0.000001) {
space = 0;
return space;
}
if (a <= 0.000001) {
space = b;
return space;
}
if (c * c >= a * a + b * b) {
space = b;
return space;
}
if (b * b >= a * a + c * c) {
space = c;
return space;
}
double p = (a + b + c) / 2;// 半周长
double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)
return space;
}
/**
* 计算两点之间的距离
*/
private double lineSpace(int x1, int y1, int x2, int y2) {
double lineLength = 0;
lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return lineLength;
}