【发布时间】:2014-01-25 19:35:43
【问题描述】:
所以我想显示我的应用用户在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
正如我所见,绘制叠加层的方法从未被调用过,而且我不知道如何修复它。
【问题讨论】:
-
您是否尝试过实现
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay;?在 iOS 7 中,mapView:viewForOverlay:是 deprecated。不过,恐怕我没有在 iOS 7 上完成足够的 MapKit 编程来帮助您实现。 -
在 drawRoute 中,当设置 pointsToUse[i] 时,您将两个参数的纬度传递给 CLLocationCoordinate2DMake。另外,你可以直接说
pointsToUse[i] = an.coordinate;。 -
@JamesFrost :谢谢,我会和这个代表核实一下
-
@Anna 没看到,改了,谢谢
标签: ios objective-c ios7 mkmapview mkoverlay