How can I change NSArray to MKPolyline polylineWithCoordinates compatible type?

I add NSTimer to record the location from the location manager and put any place in NSMutableArray.

-(void)OnTimer:(NSTimer *)param{ [self.locationRecoder addObject:self.manager.location]; } 

and I add a button to the UI, when I click the button, it calls this method

 -(IBAction)Click:(id)sender(){ NSArray *coordinateArray = [self.locationRecorder valueForKeyPath:@"coordinate"]; MKPolyline *lines = [MKPolyline ploylineWithCoordinates:(CLLocationCoordinate2D *)coordinateArray count:coordinateArray.count]; [self.map addOverlay:lines]; } 

then nothing happens. did i do something wrong in cast type?

+4
source share
1 answer

The polylineWithCoordinates method requires a simple array of C structures of type CLLocationCoordinate2D .

After calling valueForKeyPath , coordinateArray is an NSArray objects.
This is not the same as a C-array of structures.

Casting, which from NSArray to (CLLocationCoordinate2D *) does not convert it to an array of C structures.

Instead, you need to create the C array manually using malloc and loop through the locationRecoder array:

 CLLocationCoordinate2D *coordinateArray = malloc(sizeof(CLLocationCoordinate2D) * locationRecorder.count); int caIndex = 0; for (CLLocation *loc in locationRecorder) { coordinateArray[caIndex] = loc.coordinate; caIndex++; } MKPolyline *lines = [MKPolyline polylineWithCoordinates:coordinateArray count:locationRecorder.count]; free(coordinateArray); [self.map addOverlay:lines]; 
+12
source

Source: https://habr.com/ru/post/1442181/


All Articles