注释按钮不显示

时间:2013-09-05 01:34:21

标签: ios mapkit

我是第一次使用Mapkit而且我遇到了Annotation Button无法显示的问题。

我正在使用xCode 5和iOS7。我也尝试过xCode 4.6 iOS 6.1,但也没有用。

ViewController.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

ViewController.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

1 个答案:

答案 0 :(得分:1)

有几点想法:

  1. 启动位置服务时,您应该

    您不仅可以启动位置服务,还可以立即获得该位置。您确实需要等待位置服务才能调用其delegate。实际上,需要预先警告,物理设备上的位置服务实际上每次都会以更高的准确度(即较小的horizontalAccuracy值)进行几次迭代。

  2. addPin中,您实例化一个新的位置管理器对象。 (你也没有发布前一个,BTW。)你应该只使用你在viewDidLoad中创建的实例,而不是实例化一个新实例。

  3. 你的viewForAnnotation被叫了吗?我希望你在Interface Builder中将视图控制器指定为地图的delegate

  4. 看起来你有一些你没有在dealloc中发布的类属性(例如位置管理器)。我将通过静态分析器(“产品”菜单中的“分析”)运行此代码,并更正它警告您的任何内容。