Create MKPolyline with only recent CLLocations

匆匆过客 提交于 2020-01-15 19:18:26

问题


My app loads the user's past polylines and displays them on the map. Then the app starts tracking and a straight line is drawn from the last updated coordinate to the first, thereby connecting the two separate lines when they should be separate (shown here

I want to remove this straight line, so I'm thinking the easiest way would be to discard coordinates that are outside a set time frame (e.g 1 minute), so polylines on the map remain separate. I'm not sure how to do this though... I'm a beginner so any suggestions would be much appreciated!

I use this code when loading the past polyline

(IBAction)didClickLoadCoordinates:(id)sender {
// get a reference to the appDelegate so you can access the global managedObjectContext
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Route"];
NSError *error;
id results = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];

if ([results count]) {
    polyLine = (Route *)(results[0]);
    NSArray *coordinates = polyLine.coordinates;
    int ct = 0;
    for (CLLocation *loc in coordinates) {
        NSLog(@"location %d: %@", ct++, loc);
    }


    // this copies the array to your mutableArray
    _locationsArray = [coordinates mutableCopy];

}

NSInteger numberOfSteps = _locationsArray.count;

//convert to coordinates array to construct the polyline

CLLocationCoordinate2D clCoordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
    CLLocation *location = [_locationsArray objectAtIndex:index];
    CLLocationCoordinate2D coordinate2 = location.coordinate;
    clCoordinates[index] = coordinate2;
}

MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:clCoordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

And this code to just normally update the cllocations and make the polyline

//get the latest location
CLLocation *currentLocation = [locations lastObject];


//get latest location coordinates
CLLocationDegrees latitude = currentLocation.coordinate.latitude;
CLLocationDegrees longitude = currentLocation.coordinate.longitude;
CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(latitude, longitude);

//zoom map to show users location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 2000, 2000);
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];


    //store latest location in stored track array
    [_locationsArray addObject:currentLocation];


//create cllocationcoordinates to use for construction of polyline
NSInteger numberOfSteps = _locationsArray.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
    CLLocation *location = [_locationsArray objectAtIndex:index];
    CLLocationCoordinate2D coordinate2 = location.coordinate;
    coordinates[index] = coordinate2;
}

MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];

NSLog(@"%@", _locationsArray);

来源:https://stackoverflow.com/questions/25858229/create-mkpolyline-with-only-recent-cllocations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!