1
votes

I am making an app that tracks the route a user takes. I am dropping MKPointAnnotation for each location updates with horizontalAccuracy >0. Then I'm trying to draw a MKPolyline from one point to another. This begins when start button is pressed. Before pressing the start button the MKMapView shows the current location, which is correct. When I press start button I expect to see the point annotation and a polyline drawn from one point to another.

However, upon pressing start I see a polyline drawn from UTC coordinates (equator and UTC meridian) to my current location, which is undesirable.

didUpdateLocation delegate call of CLLocationManager doesn't report any such location when printed using NSLog. Here is the code...

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;
{
    CLLocationCoordinate2D coordinates[locations.count];
    int index=0;
    for (CLLocation *location in locations)
    {
        #ifdef DEBUG
        NSLog(@"received co-ordinates:\nlatitude:%f \nlongitude:%f",location.coordinate.latitude,location.coordinate.longitude);
        #endif
        
        [self centerMapWithCoordinates:location.coordinate];
        
        
        // Add an annotation
        if (location.horizontalAccuracy>0)
        {
            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
            point.coordinate = location.coordinate;
//            if (location.speed>0)
//            {
                point.title = [NSString stringWithFormat:@"%0.2f Kmph",(location.speed*3600/1000)];
//            }
            [self centerMapWithCoordinates:location.coordinate];
            [mapView addAnnotation:point];
            CLLocationCoordinate2D coordinate = location.coordinate;
            coordinates[index] = coordinate;
            index++;
        }
    }
    
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:index+1];
    [mapView addOverlay:polyline];

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay;
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];

        renderer.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
    }

    return nil;
}

What am I doing wrong? How do I get rid of this unneeded polyline?

Polyline from UTC coordinates to current location

If you give code, pleae do so in ObjC. Thanks.

Note:

I noticed after navigating app that all selected coordinates (show by pin annotations) have a polyline with respect to the same above mentioned UTC co-ordinate. See this picture...

enter image description here

1
You want one line that connects the different locations, but you are creating a new line at every location update. Why is that?Steve O'Connor
The app is going to track users walk or run workout.AceN

1 Answers

0
votes

I couldn't get the answer to this even on Apple developer forums. I opened a Technical Support Instance with Apple and turns out I was incrementing the index that would set the size of the C array to by one more than required which resulted into polylines being drawn from co-ordinates (0.0, 0.0).

I changed the code to this...

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;
{
#ifdef DEBUG
    NSLog(@"locations array %@",locations);
#endif
    for (int i=0; i<locations.count;i++)
    {
        if (i+1 <locations.count) {
            if (locations[i].horizontalAccuracy>locations[i+1].horizontalAccuracy) {
                currentLocation = locations[i+1];
            }
        }
        else
        {
            currentLocation = locations[i];
        }
        
    }
    
#ifdef DEBUG
    NSLog(@"current location: %@",currentLocation);
#endif
    [locationArray addObject:currentLocation];
    [self centerMapWithCoordinates:currentLocation.coordinate];
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = currentLocation.coordinate;
    [mapView addAnnotation:point];
    CLLocationCoordinate2D coordinates[2];
    if (previousLocation!=nil)
    {
        coordinates[0]= previousLocation.coordinate;
        coordinates[1]=currentLocation.coordinate;
        MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:2];
        [mapView addOverlay:polyline];
    }
    else
    {
        previousLocation=[currentLocation copy];
    }
    
    

}