我第一次使用 Mapkit,我遇到了注释按钮不显示的问题。
我正在使用 xCode 5 和 iOS7。我也在 xCode 4.6 iOS 6.1 中尝试过,但也没有用。
视图控制器.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
@interface ViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *name;
MKMapView *mapView;
CLLocationManager *locationManager;
NSString *dateString;
NSString *nameString;
//MKPinAnnotationView *pinView;
NSString *pinID;
}
@property(nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) UITextField *name;
-(IBAction)addPin;
-(IBAction)addName;
@end
视图控制器.m
#import "ViewController.h"
#import "PinDrop.h"
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize mapView;
@dynamic name;
- (void)viewDidLoad
{
name.delegate = self;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mapView.showsUserLocation = YES;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
//Get Location
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);
//Make Pin
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = locationManager.location.coordinate.latitude;
region.center.longitude = locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[mapView setRegion:region animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)addPin {
[self getDateTime];
//Get Location
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);
//Make Pin
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = locationManager.location.coordinate.latitude;
region.center.longitude = locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[mapView setRegion:region animated:YES];
//Pin Info
PinDrop *ann = [[PinDrop alloc] init];
nameString = name.text;
NSLog(@"Name: %@", nameString);
ann.title = [NSString stringWithFormat:@"Title: %@", nameString];
ann.subtitle = [NSString stringWithFormat:@"Date: %@", dateString];
ann.coordinate = region.center;
[mapView addAnnotation:ann];
//Clear name and stuff
name.text = @"";
}
- (NSString *)deviceLocation {
return [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[mapView.userLocation setTitle:@"I am here"];
NSLog(@"Was Else");
}
return pinView;
}
- (void)dealloc
{
[mapView release];
[super dealloc];
}
@end
PinDrop.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface PinDrop : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@end
PinDrop.m
#import "PinDrop.h"
@implementation PinDrop
@synthesize coordinate, title, subtitle;
@end