/**
* 假設地球爲一半徑爲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;
}