[英]viewforoverlay never gets called
這讓我發瘋。 我已經看過stackoveflow上的所有帖子,但是沒有什么比這合適的了。 我試圖將一條簡單的折線(即不是自定義疊加層)添加為我的MKMapView
的疊加層。 永遠不會調用委托上的viewForOverlay
方法。 為每個其他委托函數正確調用了地圖委托。 這是viewForOverlay
方法的代碼:
//maanges the overlay
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay{
NSLog(@"does it ask for the overlay view?");
MKOverlayView *overlayView = nil;
return overlayView;
}
這是構建折線並將其添加到地圖的代碼:
MKPolyline *thePolyline = [MKPolyline polylineWithPoints:pts count:[arrOfPoints count]];
[thePolyline setTitle:@"line"];
[mapView addOverlay:thePolyline];
實際上,折線確實具有我的點集合(大約1000個),所以我認為問題不存在。 我是否在地圖視圖上缺少某些必需的屬性或其他實現?
EDIT顯示用於生成折線MKMapPoint
的代碼:
我使用大約1100點的xml文件來生成折線,這是appConfig流程的一部分。 我分別使用NSXMLParser
和NSXMLParserDelegate
讀取和解析文件。 這是生成點的代碼(來自NSXMLParserDelegate
協議中的foundCharacters
方法):
//NSXMLParserDelegate methods...
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(POINT){
NSArray *arr = [string componentsSeparatedByString:@","];
MKMapPoint pt = MKMapPointMake([[arr objectAtIndex:1]doubleValue], [[arr objectAtIndex:0]doubleValue]);
MapPointObject *thePoint = [[MapPointObject alloc] init];
thePoint.mapPoint = pt;
//gives the mkmappoint to the array of points.
[arrOfPoints addObject:thePoint];
[thePoint release];
}
}
這是這些點實際生成MKPolyline
並將其提供給mapView的位置(來自NSXMLParserDelegate
協議的didEndElement
方法):
if([elementName isEqualToString:@"appConfig"]){
MKMapPoint *pts = malloc([arrOfPoints count] * sizeof(MKMapPoint));
for(int i = 0; i <= [arrOfPoints count] - 1; i++){
MapPointObject *pointObject = [arrOfPoints objectAtIndex:i];
pts[i] = pointObject.mapPoint;
}
MKPolyline *thePolyline = [MKPolyline polylineWithPoints:pts count:[arrOfPoints count]];
[thePolyline setTitle:@"line"];
//adding the polyline to the model's mapview
Model *theModel = [Model sharedModel];
[theModel.mapView setVisibleMapRect:thePolyline.boundingMapRect animated:YES];
[theModel.mapView addOverlay:thePolyline];
free(pts);
}
實際上, MKPolyline
上的點數屬性確實表明其中有1100點。
編輯:示例XML值:
<appConfig>
<point>-94.847587,38.977967</point>
<point>-94.844111,38.977978</point>
<point>-94.844108,38.977369</point>
<point>-94.844003,38.977369</point>
<point>-94.843955,38.974886</point>
xml文件包含坐標(緯度和經度)值。 這些坐標值與MKMapPoint
值不同( MKMapPoint
值是地圖視圖的經度/緯度在平面地圖上的x,y投影)。
您應該存儲坐標,而不是MKMapPoint
值。
因此,不要使用MKMapPoint
和polylineWithPoints
,而是使用CLLocationCoordinate2D
和polylineWithCoordinates
。
在XML解析器方法,創建和存儲CLLocationCoordinate2D
使用CLLocationCoordinate2DMake
。
pts
數組的類型應為CLLocationCoordinate2D *
並且在執行malloc
,請使用sizeof(CLLocationCoordinate2D)
。
然后調用polylineWithCoordinates
而不是polylineWithPoints
。
順便說一句,在進行上述更改之后,您還需要在viewForOverlay
實際返回一個非nil的覆蓋視圖,否則您仍然看不到該行:
MKPolylineView *polylineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 2.0;
return polylineView;
我解決了問題,但是鑒於@AnnaKarenina的原始解決方案已解決了該問題,因此將其標記為答案。 我只是發布此信息,以使其他信息可用於其后的其他任何人。
當我最初編寫代碼時,MKMapPointMake接受如下參數: MKMapPointMake(x,y)
。 當我切換它時, CLLocationCoordinate2DMake
采用類似這樣的參數: CLLocationCoordinate2DMake(lat,lng)
,或者用笛卡爾術語來說, CLLocationCoordinate2DMake(y,x)
。 對於我特定的一組坐標對,這個不存在,因此它從未調用過viewForOverlay委托方法。 我更改了CLLocationCoordinate2DMake構造函數的參數順序,現在一切正常。
[self.mapView addOverlay:polyline];
[self.mapView setNeedsDisplay];
通過調用setNeedsDisplay來強制刷新mapView:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.