如何将NSArray更改为MKPolyline polylineWithCoordinates兼容类型? [英] How can i change the NSArray to MKPolyline polylineWithCoordinates compatible type?
问题描述
我添加了一个NSTimer来记录位置管理器的位置,并将所有位置放入NSMutableArray。
I add a NSTimer to record the location from location manager ,and put ever location into a NSMutableArray.
-(void)OnTimer:(NSTimer *)param{
[self.locationRecoder addObject:self.manager.location];
}
我向UI添加一个按钮,当我点击按钮时,它会调用这个方法
and I add a button to UI, when I click the button, it invoke 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 there is nothing drawn. did i do something wrong in type cast?
推荐答案
polylineWithCoordinates
方法需要一个简单的C数组结构 CLLocationCoordinate2D
。
The polylineWithCoordinates
method requires a plain C array of structs of type CLLocationCoordinate2D
.
调用 valueForKeyPath之后
, coordinateArray
是一个 NSArray
NSValue
对象。
这与C数组结构不同。
After the call to valueForKeyPath
, coordinateArray
is an NSArray
of NSValue
objects.
That is not the same thing as a C array of structs.
转换 NSArray
到(CLLocationCoordinate2D *)
不会将其转换为结构的C数组。
Casting that NSArray
to (CLLocationCoordinate2D *)
doesn't convert it to a C array of structs.
相反,您必须使用 malloc
手动创建C数组并循环遍历 locationRecoder
数组:
Instead, you have to create the C array manually using malloc
and looping 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];
这篇关于如何将NSArray更改为MKPolyline polylineWithCoordinates兼容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!