我會嘗試在三個步驟來解決這個問題:
步驟1.將每個座標用戶的軌道才能CGPoint
並保存一個數組。
// in viewDidLoad
locManager = [[CLLocationManager alloc] init];
[locManager setDelegate:self];
[locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locManager startPdatingLocation];
self.userCoordinatePoints = [NSMutableArray alloc]init];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D loc = [newLocation coordinate];
CGPoint *currentPoint = [self.mapView convertCoordinate:loc toPointToView:self.mapView];
// add points to array
self.userCoordinatePoints addObject:currentpoint];
}
第2步。轉換MKPolylineView
到CGPathRef
創建類型CGPathRef
{
CGPathRef path;
}
這種方法,你必須實現兩點之間創建路線之類變量:
- (MKOverlayView*)mapView:(MKMapView*)theMapView
viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *overlayView = [[MKPolylineView alloc]
initWithOverlay:overlay];
overlayView.lineWidth = 3;
overlayView.strokeColor = [[UIColor blueColor]colorWithAlphaComponent:0.5f];
// overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1f];
path = overlayView.path;
return overlayView;
}
步驟3:創建一個自定義的方法來檢查無論點是否在CGPath
- (BOOL)userRouteIntersectsGoogleRoute
{
// Loop through all CGPoints
for(CGPoint point in self.userCoordinatePoints)
{
BOOL val = CGPathContainsPoint(path, NULL, point);
if(val)
{
return YES;
}
}
return NO;
}
您是否試圖找出用戶是否交叉(通過位置服務的實際位置),還是試圖讓用戶獲取路線並嘗試找出這些路線是否通過特定線路? –
@TimBodeit我想找到用戶是否穿過線(例如,如果他已經通過終點線) – ghostrider