Reputation: 2087
Im working with Mapkit for the first time and i am having a problem of Annotation Button not displaying.
I am using xCode 5 and iOS7. I also did try in xCode 4.6 iOS 6.1 and that did not work either.
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
Upvotes: 1
Views: 336
Reputation: 437632
A couple of thoughts:
When starting location services, you should
specify the delegate of self
; and
the view controller should implement the CLLocationManagerDelegate
call, didUpdateLocations
You can't just start location services and then expect to immediately have the location. You really need to wait for location services to call its delegate
. In fact, be forewarned that the location services on a physical device actually takes a couple of iterations with increasing accuracy (i.e. smaller horizontalAccuracy
values) each time.
In your addPin
, your instantiating a new location manager object. (You're also not releasing the prior one, BTW.) You should just use the instance that you created in viewDidLoad
, not instantiate a new one.
Is your viewForAnnotation
getting called? I hope that you specified the view controller as the map's delegate
in Interface Builder.
It looks like you have some class properties that you're not releasing in dealloc
(e.g. the location manager). I'd run this code through the static analyzer ("Analyze" on the "Product" menu), and correct anything it warns you about.
Upvotes: 1