gpt4 book ai didi

ios - MKPolyline 未显示在 MKMapView 上

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:47 26 4
gpt4 key购买 nike

这是代码。它非常简单。我正在为走路的人开路。所以,这是我的 ViewController.m 文件的代码:

#import "ViewController.h"

@interface ViewController ()

@property BOOL firstTime;

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.mapView setDelegate:self];
[self.mapView setShowsUserLocation:YES];
[self.mapView setMapType:MKMapTypeHybrid];

[self setLocationManager:[[CLLocationManager alloc] init]];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager startUpdatingLocation];

self.index = 0;

self.firstTime = YES;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if(self.firstTime)
{
CLLocation *startingLocation = [locations objectAtIndex:0];
self.startingPointCooridinates = startingLocation.coordinate;
self.index++;

MKPointAnnotation *startingPointAnnotation = [[MKPointAnnotation alloc] init];
startingPointAnnotation.title = @"Starting Point";
startingPointAnnotation.coordinate = startingLocation.coordinate;

[self.mapView addAnnotation:startingPointAnnotation];

self.firstTime = false;
}

[self.locations addObject:[locations objectAtIndex:0]];

CLLocationCoordinate2D coordinates[[self.locations count]];
for(int i = 0; i < self.locations.count; i++)
{
CLLocation *currentLocation = [locations objectAtIndex:i];
coordinates[i] = currentLocation.coordinate;
}

MKPolyline *pathPolyline = [MKPolyline polylineWithCoordinates:coordinates count:self.locations.count];
[self.mapView addOverlay:pathPolyline];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineRenderer *polylineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
polylineRenderer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
polylineRenderer.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
polylineRenderer.lineWidth = 2.0;
return polylineRenderer;
}
else
{
return nil;
}
}

现在,只显示注释,没有显示任何 MKPolyline。我究竟做错了什么 ?谢谢。

最佳答案

  1. 如评论中所述,您的 locations 数组从未分配和初始化,因此它是 nil 并调用它(如 addObject) 什么都不做,因此多段线永远不会添加任何坐标(因此它不会显示)。

    viewDidLoad 中,在启动 CLLocationManager 之前,分配并初始化数组:

    self.locations = [NSMutableArray array];
  2. 您将遇到的另一个问题是 for 循环中 didUpdateLocations 中的这一行:

    CLLocation *currentLocation = [locations objectAtIndex:i];

    这里,locations(没有self.)指的是委托(delegate)方法的局部参数变量,而不是您的位置 类实例级属性变量。编译器必须通过类似“'locations' 的局部声明隐藏实例变量”的消息警告您这一点。

    在这种情况下,警告很严重。您在这里真正想做的是引用 locations 属性变量,您在其中存储用户的完整坐标轨迹,而不是仅具有最后 x 个未报告位置的局部变量(通常只有 1 个对象).

    因此该行应更改为:

    CLLocation *currentLocation = [self.locations objectAtIndex:i];

    最好使用与 locations 不同的名称来避免这些问题。

  3. 正如评论中所提到的,由于每次用户移动时您都在添加一个包含用户完整运动轨迹的叠加层,因此您需要先删除之前的叠加层。否则,您将不必要地向 map 添加多个叠加层,因为最后一个叠加层覆盖了整个运动。之前的叠加层不明显可见,因为它们具有相同的坐标、相同的颜色和相同的线宽。因此,在调用 addOverlay 之前,最简单的做法是使用 map 的当前叠加层列表调用 removeOverlays:

    //remove any previous overlays first...
    [self.mapView removeOverlays:mapView.overlays];

    //now add overlay with the updated trail...
    [self.mapView addOverlay:pathPolyline];
  4. 不影响显示的一个小问题是为折线设置 fillColor 没有效果。您只需要设置代码正在执行的 strokeColor 即可。您可以删除设置 fillColor 的调用。

  5. 最后,您可能有兴趣查看 Apple's Breadcrumb sample app .他们的版本使用自定义覆盖层,可以动态更新,而无需在每次更改或添加时删除和添加覆盖层。

关于ios - MKPolyline 未显示在 MKMapView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20721190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com