我正在使用 MKMapView、注释、覆盖等,但我在 MKMapPointForCoordinate() 返回无效坐标的对接问题上遇到了麻烦。
代码:
MKMapPoint* pointArr;
for (Category* route in validRoutes){
NSString* routeID = [route routeid];
NSArray* pointData = [routes objectForKey:routeID];
pointArr = malloc(sizeof(MKMapPoint) * [pointData count]);
int i = 0;
for (NSDictionary* routeData in pointData) {
NSString* latitude = [routeData objectForKey:@"latitude"];
NSString* longitude = [routeData objectForKey:@"longitude"];
NSLog(@"L: %@ L: %@",latitude, longitude);
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[f numberFromString:latitude] doubleValue], [[f numberFromString:longitude] doubleValue]);
NSLog(@"Coord: %f %f",coord.latitude,coord.longitude);
MKMapPoint point = MKMapPointForCoordinate(coord);
NSLog(@"Point: %f %f",point.x,point.y);
pointArr[i] = point;
i++;
}
MKPolyline *polyline = [MKPolyline polylineWithPoints:pointArr count: i];
polyline.title = [route name];
[routeOverlays setObject:polyline forKey: [route routeid]];
[map addOverlay:polyline];
free(pointArr);
}
输出示例:
大号:41.380840 大号:-83.641319
坐标:41.380840 -83.641319
点:71850240.204982 100266073.824832
我不明白为什么转换为 MKMapPoint 会破坏我的 CLLocationCoordinate2D 的值。叠加层未显示在地图上,因为值无效...
编辑:我通过使用 MKMapPointMake 得到了重点,但是,我的叠加层仍然没有显示。这是mapView:viewForOverlay:代码:
-(MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
MKOverlayView *overlayView = nil;
//Checks if the overlay is of type MKPolyline
if([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:overlay];
routeLineView.strokeColor = [UIColor orangeColor];
routeLineView.lineWidth = 10;
return overlayView;
}
return nil;
}
该方法被调用(使用断点确认),并且我有注释工作(所以必须正确设置委托,我假设)
双重编辑::facepalm:我每次在委托代码中都返回 nil。这就是我复制和粘贴以前版本代码的结果;P