ios - Mapkit 上的图钉未显示
问题描述
我试图在地图上添加一个简单的图钉,但不知何故,无论我添加哪种图钉,它都没有显示出来。我的代码有问题吗?
我的代码:
#define METERS_MILE 1609.344
#define METERS_FEET 3.28084
#import "ViewController.h"
#import "../Jetfire_websocket/JFRWebSocket.h"
#import "MyCustomAnnotation.h"
@interface ViewController ()
<CLLocationManagerDelegate>
@end
@interface ViewController ()<JFRWebSocketDelegate>
@property(nonatomic, strong)JFRWebSocket *socket;
@end
@interface ViewController () <MKMapViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
// Location services.
[[self mapView] setShowsUserLocation:YES];
self.locationManager = [[CLLocationManager alloc] init];
[[self locationManager] setDelegate:self];
// Need to setup the location manager with permission in iOS versions later than ios8.
if ([[self locationManager] respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[[self locationManager] requestWhenInUseAuthorization];
}
[[self locationManager] setDesiredAccuracy:kCLLocationAccuracyBest];
[[self locationManager] startUpdatingLocation];
NSLog(@"Custom Annotations start.");
// Custom Annotations.
CLLocationCoordinate2D bryanParkCoordinates = CLLocationCoordinate2DMake(37.785834, -122.406417);
MyCustomAnnotation *bryanParkAnnotation = [[MyCustomAnnotation alloc]initWithTitle:@"Bryant Park" Location:bryanParkCoordinates];
[self.mapView addAnnotation:bryanParkAnnotation];
// Simple pin.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:bryanParkCoordinates];
[annotation setTitle:@"Title"];
[self.mapView addAnnotation:annotation];
// Another try.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
MKPointAnnotation *newannotation = [[MKPointAnnotation alloc] init];
[newannotation setCoordinate:coord];
[self.mapView setRegion:region];
[self.mapView addAnnotation:newannotation];
// On your location.
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = bryanParkCoordinates;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];
// Geocoding
self.searchResult.text = @"default text";
[self.searchButton setTitle:@"Search Location Name" forState:(UIControlStateNormal)];
// websocket connection initialization.
self.socket = [[JFRWebSocket alloc] initWithURL:[NSURL URLWithString:@"ws://localhost:9002"] protocols:@[@"chat",@"superchat"]];
self.socket.delegate = self;
[self.socket connect];
// Firebase initialization.
self.ref = [[FIRDatabase database] reference];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// if([annotation isKindOfClass:[MyCustomAnnotation class]])
// {
// MyCustomAnnotation *myLocation = (MyCustomAnnotation *)annotation;
// MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MyCustomAnnotation"];
// if(annotationView == nil)
// annotationView = myLocation.annotationView;
// else
// annotationView.annotation = annotation;
// return annotationView;
// }
// else{
// MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DETAILPIN_ID"];
// [pinView setAnimatesDrop:YES];
// [pinView setCanShowCallout:NO];
// return pinView;
// //return nil;
// }
if (annotation == mapView.userLocation) return nil;
static NSString* Identifier = @"PinAnnotationIdentifier";
MKPinAnnotationView* pinView;
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:Identifier];
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:Identifier];
pinView.canShowCallout = YES;
return pinView;
}
pinView.annotation = annotation;
return pinView;
}
我在这里遵循了在地图上添加图钉的简单解决方案:快速将单个图钉添加到 MKMapView? 但它仍然没有出现。
解决方案
通过删除对自定义注释的引用,我能够让您的代码正常工作。因此,仅使用MKPointAnnotation
该类就(可能)解决了冲突。
下面的代码在viewDidLoad
CLLocationCoordinate2D aCoord = CLLocationCoordinate2DMake(37.785834, -122.406417);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = aCoord;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {aCoord, span};
[self.mapView setRegion:region];
CLLocationCoordinate2D bCoord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKPointAnnotation *anotherPoint = [[MKPointAnnotation alloc] init];
anotherPoint.coordinate = bCoord;
anotherPoint.title = @"Title?";
anotherPoint.subtitle = @"Subtitle!!!";
[self.mapView addAnnotation:point];
[self.mapView addAnnotation:anotherPoint];
推荐阅读
- r - 在 RStudio 的 glm() 中为两个不同的模型获得相同的结果
- excel - 将 excel 与另一个具有动态选项卡名称的已关闭 excel 链接
- javascript - 使用 Javascript Dropbox API 上传带有块的大文件
- google-cloud-platform - 如何从 GCP Bucket 提供静态文件并从任何未找到的路径重定向到 index.html,状态为 200 而不是 404
- javascript - 在两个对象之间映射值并读取值
- ruby-on-rails - 在机械化中按占位符查找元素
- javascript - Apps 脚本 -> 由于浏览器更新,在主页上加载 iframe 表单时出错
- python - Lambda:创建实例:启动后运行命令
- arrays - 打印数组只导致第一个值被打印到excel中?
- sql - SQL 创建具有真/假列的视图