2012-05-18 101 views
0

內我有其中包含的用戶的當前位置CLLocation對象和我有4緯度/經度對用於可傾斜的矩形的每一個角。現在我想檢查CLLocation座標是否在該矩形內。IOS:緯度/經度矩形

以下是我已經嘗試了矩形

#define NorthEast_LAT 51.514894 
#define NorthEast_LNG -0.135306 
#define SouthEast_LAT 51.514831 
#define SouthEast_LNG -0.135153 
#define NorthWest_LAT 51.514719 
#define NorthWest_LNG -0.135858 
#define SouthWest_LAT 51.514556 
#define SouthWest_LNG -0.135714 

的下面的代碼座標,但是我認爲,當矩形的角度爲0度時纔會有效。

BOOL withinRect = [delegate.CLController latlngWithInBox:location 
                point1:CLLocationCoordinate2DMake(NorthEast_LAT, NorthEast_LNG) 
                point2:CLLocationCoordinate2DMake(SouthEast_LAT, SouthEast_LNG) 
                point3:CLLocationCoordinate2DMake(NorthWest_LAT, NorthWest_LNG) 
                point4:CLLocationCoordinate2DMake(SouthWest_LAT, SouthWest_LNG)]; 

- (BOOL) latlngWithInBox:(CLLocation *)position point1:(CLLocationCoordinate2D)point1 point2:(CLLocationCoordinate2D)point2 point3:(CLLocationCoordinate2D)point3 point4:(CLLocationCoordinate2D)point4 { 

if (position.coordinate.latitude >= point3.latitude && position.coordinate.latitude <= point2.latitude 
    && position.coordinate.longitude >= point3.longitude && position.coordinate.longitude <= point2.longitude) { 
    return YES; 
} 
return NO; 

}

+1

Ewwww ......一個建議是使用'CLLocationCoordinate2D'代替NSNumbers陣列第一。將使你的代碼更容易理解。另外,你不要考慮'point1'和'point2'。 – DarkDust

+0

是的,我應該改變代碼通過CLLocationCoordinate2D代替陣列 –

+0

代碼改變... –

回答

0
- (BOOL) latlngWithInBox:(CLLocation *)position point1:(CLLocationCoordinate2D)point1 point2:(CLLocationCoordinate2D)point2 point3:(CLLocationCoordinate2D)point3 point4:(CLLocationCoordinate2D)point4 { 
    //&& position.coordinate.latitude >= [[point4 objectAtIndex:0] floatValue] && position.coordinate.latitude <= [[point1 objectAtIndex:0] floatValue] && position.coordinate.longitude <= [[point4 objectAtIndex:1] floatValue] && position.coordinate.longitude >= [[point1 objectAtIndex:1] floatValue] 
    if (PointInTriangle(position.coordinate, point1, point2, point3) || PointInTriangle(position.coordinate, point2, point3, point4)) { 
     return YES; 
    } 
    return NO; 
} 

float sign(CLLocationCoordinate2D p1, CLLocationCoordinate2D p2, CLLocationCoordinate2D p3) 
{ 
    return (p1.longitude - p3.longitude) * (p2.latitude - p3.latitude) - (p2.longitude - p3.longitude) * (p1.latitude - p3.latitude); 
} 

bool PointInTriangle(CLLocationCoordinate2D pt, CLLocationCoordinate2D v1, CLLocationCoordinate2D v2, CLLocationCoordinate2D v3) 
{ 
    bool b1, b2, b3; 

    b1 = sign(pt, v1, v2) < 0.0f; 
    b2 = sign(pt, v2, v3) < 0.0f; 
    b3 = sign(pt, v3, v1) < 0.0f; 

// NSLog(@"b1-%@", [NSNumber numberWithBool:b1]); 
// NSLog(@"b2-%@", [NSNumber numberWithBool:b2]); 
// NSLog(@"b3-%@", [NSNumber numberWithBool:b3]); 

    return ((b1 == b2) && (b2 == b3)); 
} 
+0

上述解決方案爲我 –

1

讓我們忽視了對地理座標的矩形是一個球體(數學很難有)。所以你想知道一個點是否在四邊形內。

最簡單的方式是首先添加一個限制:點具有以特定的順序(NE,NW,SE,SW),以進行說明。然後,將它們視爲正常的二維座標(經度= x,緯度= y)。

下一步是減少的問題:讓座標形式2個三角形,例如NE-NW-SE和NW-SE-SW。然後檢查你的point is within one of those two triangles

+0

不錯,簡短的解決方案,但它的工作原理。你有沒有任何示例代碼?它是x =經度還是x =緯度? –

+0

不,我沒有理會編寫代碼示例,你應該能夠自己做:-)經度=東西= X,緯度=南北= Y。 – DarkDust

+0

我寫了一個代碼。如果沒關係,你可以請c。它一直沒有給出正確的結果。看到我的答案。 –

6

另一種方式來確定某個點是否是地圖矩形內是使用MKMapKit的功能:

  1. MKMapPointForCoordinate - 轉換座標映射點
  2. MKMapRectMake - 用這些點來創建矩形
  3. MKMapRectContainsPoint - 確定指定的地圖點是否位於矩形內的

其優點是MKMapKit(MKMapPoint,MKMapRect)使用地圖的墨卡託投影,因此您不需要提供球體計算。但是在iOS 4.0及更高版本中可以使用其中的一些功能。

UPDATE:

// 1 ------- 2 
// |   | 
// |  x | 
// |   | 
// 3 ------- 4  

// 1 = topLeftCorner 
// 4 = bottomRightCorner 
// x = targetCoordinate 

CLLocationCoordinate2D topLeftCorner = /* some coordinate */, bottomRightCorner = /* some coordinate */; 
CLLocationCoordinate2D targetCoordinate = /* some coordinate */; 
MKMapPoint topLeftPoint = MKMapPointForCoordinate(topLeftCorner); 
MKMapPoint bottomRightPoint = MKMapPointForCoordinate(bottomRightCorner); 
MKMapRect mapRect = MKMapRectMake(topLeftPoint.x, topLeftPoint.y, bottomRightPoint.x - topLeftPoint.x, bottomRightPoint.y - topLeftPoint.y); 
MKMapPoint targetPoint = MKMapPointForCoordinate(targetCoordinate); 

BOOL isInside = MKMapRectContainsPoint(mapRect, targetPoint); 
+0

如何使用四個經緯度對創建MKMapRect? –

+0

另外我將如何從CLLocationCoordinate2d創建MKMapPoint? –

+0

在我的更新中查找答案。 – SVGreg