gpt4 book ai didi

ios - MapKit 注释未出现

转载 作者:行者123 更新时间:2023-12-03 17:21:05 25 4
gpt4 key购买 nike

我在 iOS 应用程序的 map 上显示注释时遇到问题。

我遵循了文档中的约定。从调试来看,似乎从未为注释点调用 viewForAnnotation。

View Controller 的代码是:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super
// Do any additional setup after loading the view, typically from a nib.

self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

[self.locationManager requestAlwaysAuthorization];

[self.locationManager startUpdatingLocation];

self.mapView.showsUserLocation = YES;
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];

self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];


//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[self.mapView setRegion:region animated:YES];


[self.mapView addAnnotations:[self createAnnotations]];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];

}

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

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation");

if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}

MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] init];

pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"foo"];

[pinView setPinColor:MKPinAnnotationColorGreen];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;

return pinView;
}

- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];
return annotations;
}

@end

最佳答案

一个明显的问题出现在 createAnnotations 方法中:

CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];


注释的坐标属性永远不会被设置。

创建注释后设置:

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coord; // <-- add this line
[annotations addObject:annotation];

关于ios - MapKit 注释未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338349/

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