質問

I have a map view where the user location is tracked, I am trying to add the user location coordinates into an array and display them in a textview as a test but the most recent coordinates are being displayed, I want them to show up as a list in the textview, similar to the NSlog. I think Im missing something but don't know what.

    _trackLat = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
    _trackLong = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    _trackCoord = [NSString stringWithFormat:@"%@, %@", _trackLat, _trackLong];
    _trackArray = [[NSMutableArray alloc] initWithObjects:_trackCoord, nil];
    self.textView.text = self.trackCoord;
    NSLog(@"coord lat = %f, coord long: %f",newLocation.coordinate.latitude, newLocation.coordinate.longitude);
役に立ちましたか?

解決

you should create your array once:

NSMutableArray *trackArray = [NSMutableArray array];

because (0x7fffffff rightly mentioned) your array gets release when you set your pointer to your new array created by [[NSArray alloc] init...].

with each new coordinate do:

[trackArray addObject: [NSString stringWithFormat: @"%f, %f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude]];
self.textView.text = [self.textView.text stringByAppendingString: trackArray.lastObject];

他のヒント

There are a couple of things I'd like to point out here, the first of which being that it looks like you're alloc/initing the array every time you attempt to add new coordinates. When you do this, you discard the previous contents of the array. Then you never actually assign the array as the text field's text.

As a side note, you don't have to construct strings to hold the coordinates on your own. You can simply use NSStringFromCGPoint(), and then pass in your latitude and longitude.

NSString *coordinate = NSStringFromCGPoint(CGPointMake(newLocation.coordinate.longitude, newLocation.coordinate.latitude));

if (!self.trackArray) {
    self.trackArray = [[NSMutableArray alloc] init];
}

[self.trackArray addObject:coordinate];
[self.textView setText:[self.trackArray description]];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top