【发布时间】:2018-12-31 04:54:54
【问题描述】:
我尝试在地图上添加一个简单的图钉,但不知何故,无论我添加哪种图钉,它都没有显示出来。我的代码有问题吗?
我的代码:
#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;
}
我在这里遵循了在地图上添加图钉的简单解决方案:Quickly adding single pin to MKMapView? 但它仍然没有出现。
【问题讨论】:
标签: ios objective-c mapkit