[英]mapView viewForOverlay never called
所以我想顯示我的應用程序用戶在MKMapView
上走的位置,我使用以下代碼收集數據:
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
// calc. distance walked
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
self.totalMetters += meters;
[[self labelDistance] setText:[self formatDistanceIntoString:self.totalMetters]];
// create another annotation
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = newLocation.coordinate;
// Also add to our map so we can remove old values later
[self.locations addObject:annotation];
// Remove values if the array is too big
while (self.locations.count > 100)
{
annotation = [self.locations objectAtIndex:0];
[self.locations removeObjectAtIndex:0];
// Also remove from the map
[self.map removeAnnotation:annotation];
}
一旦完成,我稱之為draw方法:
[self drawRoute];
其中包含以下內容:
- (void)drawRoute {
NSLog(@"drawRoute");
NSInteger pointsCount = self.locations.count;
CLLocationCoordinate2D pointsToUse[pointsCount];
for(int i = 0; i < pointsCount; i++) {
MKPointAnnotation *an = [self.locations objectAtIndex:i];
pointsToUse[i] = CLLocationCoordinate2DMake(an.coordinate.latitude,an.coordinate.latitude);
}
MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount];
[self.map addOverlay:myPolyline];
}
最后我的mapView
委托:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
NSLog(@"route");
if ([overlay isKindOfClass:MKPolyline.class]) {
MKPolylineView *lineView = [[MKPolylineView alloc] initWithOverlay:overlay];
lineView.strokeColor = [UIColor greenColor];
return lineView;
}
return nil;
}
顯然我的控制器是MKMapView
Delegate符合
@interface NewWalkViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
Storyboard
的mapView
鏈接到控制器(插座和代理)
我使用“自行車道”調試工具,有輸出:
2014-01-25 20:27:30.132遛狗[2963:70b]新址:37.330435
2014-01-25 20:27:30.133遛狗[2963:70b] drawRoute
正如我所看到的,繪制疊加層的方法永遠不會被調用,而且我沒有一個線索如何解決它。
主要問題是在drawRoute
,這一行將兩個參數的latitude
傳遞給CLLocationCoordinate2DMake
:
pointsToUse[i] = CLLocationCoordinate2DMake
(an.coordinate.latitude,an.coordinate.latitude);
這導致在世界的不同部分繪制線條而不是實際的an.coordinate
。 例如,如果an.coordinate
是37,-122(舊金山附近的某個地方),則該線被繪制為37,37(土耳其南部某處)。
由於您實際上並未將地圖定位在錯誤的位置(您正在尋找“右側”位置的線), viewForOverlay
從不調用viewForOverlay
因為地圖僅在覆蓋可能可見時才調用它。
將該行更改為:
pointsToUse[i] = CLLocationCoordinate2DMake
(an.coordinate.latitude,an.coordinate.longitude);
或者干脆:
pointsToUse[i] = an.coordinate;
正如James Frost在評論中提到的,從iOS 7開始,你應該使用rendererForOverlay
而不是viewForOverlay
盡管如果還沒有實現rendererForOverlay
,地圖視圖仍將在iOS 7中調用viewForOverlay
。 雖然這不會阻止您的疊加顯示在當前情況下,但您應該實現新的委托方法以及舊委托方法(如果iOS 7應用程序也將在iOS 6或更早版本上運行)。
另一個重要但不相關的問題是您不必要地創建多個重疊的疊加層。 在drawRoute
,由於您要創建的疊加層包含所有位置,因此在添加新疊加層之前應刪除所有疊加層。 否則,地圖最終會顯示位置0到1的疊加層,位置0到位置2的疊加層,位置0到位置3的疊加層等。之前的疊加層顯然不可見,因為它們具有相同的顏色和線條寬度。 在addOverlay
行之前,放入:
[self.map removeOverlays:self.map.overlays];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.