Pregunta

In my app I count the footsteps and now I want to know the speed and I want to store the GPS coordinates to draw a polyline in another ViewController. I thought that I can store this coordinate by using the following code:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        locationArray = [[NSMutableArray alloc]init];
        [locationArray addObject:currentLocation];
        speed = (int) currentLocation.speed * 3.6;
        self.labelSpeed.text = [NSString stringWithFormat:@"%d Km/h",speed];
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
    }
}

But it doesn't works, because in my locationArray it stored only the last coordinates received by the GPS sensor. To explain you better what I'm trying to develop I will write here something much specific: In the first ViewController I want to show 2 label in which I calculate the number of steps and the speed. So in this ViewController I've to receive the coordinates data and I thought this data should be inserted in a NSMutableArray. In the second ViewController I show a label in which I will insert the total number of steps (by using a prepareForSegue method) and below a MapView in which I will draw a polyline to display the path I made. For that I need the coordinates that I received in the first ViewController, so I've to pass the data from the first to the second ViewController by using prepareForSegue method. My issue is how I can store all the coordinates to have them in the second ViewController to draw the polyline? Cna anyone help me?

Thank you

¿Fue útil?

Solución

You are storing just the last coordinates because you are initializing your array every time you get a new location, move the allocation line to another method (like viewDidLoad) and erase that line in your didUpdateToLocation.

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationArray = [[NSMutableArray alloc]init];
    //.... more code
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        //locationArray = [[NSMutableArray alloc]init]; //This line doesn't go here
        [locationArray addObject:currentLocation];
        speed = (int) currentLocation.speed * 3.6;
        self.labelSpeed.text = [NSString stringWithFormat:@"%d Km/h",speed];
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top