在我的應用程序中,我有一個圓周的座標數組(CLLocationCoordinate2D)。現在我從某處獲得座標,並且需要檢查新座標是否落在圓內。 如果是這樣,我只是想在該座標上顯示一個針,並且基本上它應該落在該區域下。 我該如何去檢查這個?如何檢測GPS座標是否落在MKMapView的某個區域?
感謝,
在我的應用程序中,我有一個圓周的座標數組(CLLocationCoordinate2D)。現在我從某處獲得座標,並且需要檢查新座標是否落在圓內。 如果是這樣,我只是想在該座標上顯示一個針,並且基本上它應該落在該區域下。 我該如何去檢查這個?如何檢測GPS座標是否落在MKMapView的某個區域?
感謝,
你沒有說,如果你的座標陣列只是緯度和經度,CLLocationCoordinate2D
結構或CLLocation
對象,但如果你創建一個CLLocation
對象(如果你不已經有一個) ,然後您可以致電distanceFromLocation
查看距離其他地點有多遠。我假設,除了外圍的座標數組之外,還有中心的座標?
如果你有CLLocationCoordinate2D
,你可以因此做:
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude
longitude:coordinate.longitude];
CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:centerCoordinate.latitude
longitude:centerCoordinate.longitude];
CLLocationDistance distance = [location distanceFromLocation:centerLocation];
與你離線聊天,這聽起來像在地圖上的座標的陣列是一個用戶手勢的結果(這可以解釋爲什麼你沒」 t具有座標圓的中心)。
鑑於此情況,而不是使用映射方法試圖找出區域是否包含座標,我建議使用Quartz方法來測試視圖中的CGPoint
是否包含在關閉UIBezierPath
,我們將從用戶的手勢構建。
因此:
構建UIBezierPath
用戶拖動在屏幕上它們的手指;
完成後,將結果路徑與所討論的座標進行比較。例如,地圖視圖顯示用戶位置,您可以查看地圖的userLocation
屬性,將其座標從CLLocationCoordinate2D
轉換爲使用地圖視圖的convertCoordinate:toPointToView
方法在視圖中查看座標。
具有CGPoint
的MapView用戶的當前位置內,我們現在可以使用UIBezierPath
實例方法,containsPoint
測試,看看如果該點是貝塞爾路徑中。
因此,可能看起來像:
- (void)turnOnGestureForView:(MKMapView *)mapView
{
mapView.scrollEnabled = NO;
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[mapView addGestureRecognizer:gesture];
}
- (void)turnOffGesture:(UIGestureRecognizer *)gesture map:(MKMapView *)mapView
{
[mapView removeGestureRecognizer:gesture];
mapView.scrollEnabled = YES;
}
- (void)handleGesture:(UIPanGestureRecognizer *)gesture
{
static UIBezierPath *path;
static CAShapeLayer *shapeLayer;
CGPoint location = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateBegan)
{
if (!shapeLayer)
{
shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
shapeLayer.strokeColor = [[UIColor redColor] CGColor];
shapeLayer.lineWidth = 3.0;
[self.mapView.layer addSublayer:shapeLayer];
}
path = [UIBezierPath bezierPath];
[path moveToPoint:location];
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
[path addLineToPoint:location];
shapeLayer.path = [path CGPath];
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
MKMapView *mapView = (MKMapView *)gesture.view;
[path addLineToPoint:location];
[path closePath];
CGPoint currentLocation = [mapView convertCoordinate:mapView.userLocation.coordinate
toPointToView:gesture.view];
if ([path containsPoint:currentLocation])
NSLog(@"%s path contains %@", __FUNCTION__, NSStringFromCGPoint(currentLocation));
else
NSLog(@"%s path does not contain %@", __FUNCTION__, NSStringFromCGPoint(currentLocation));
[shapeLayer removeFromSuperlayer];
shapeLayer = nil;
// if you want to turn off the gesture and turn scrolling back on, you can do that now
[self turnOffGesture:gesture map:mapView];
}
}
對不起that.Its CLLocationCoordniate2D。 – Ashutosh
我沒有中心的座標。即使我得到了距離,我怎麼知道它是否在邊界之下。請解釋我知道你要去哪裏,但仍然無法在代碼中想到它。 – Ashutosh
是的,我的意思是在圈子的外圍。 – Ashutosh