Gettin Bad Access while adding Annotations - ios

I´m trying to to add many Annotations (depends on how many objects a have in my Array) to my Mapview like this:
-(void)viewWillAppear:(BOOL)animated
{
[mapView removeAnnotations:mapView.annotations];
for (Daten *info in datenArray) {
CLLocationCoordinate2D location;
location.latitude = (double)[info.lati doubleValue];
location.longitude = (double)[info.longi doubleValue];
MapPin *newAnnotation =[[[MapPin alloc] initWithTitle:info.rating andCoordinate:location] autorelease];
[mapView addAnnotation:newAnnotation];
}
[self zoomToFitMapAnnotations:self.mapView];
[self.mapView selectAnnotation:self.mapView.annotations.lastObject animated:YES];
}
The first time I switch to that View it works perfectly...But if i come a second time to this View, i get this Bad Access with Error:
*** -[CFString length]: message sent to deallocated instance 0x17e140c0
Edit:
Thats my MapPin Class:
#interface MapPin : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly, copy) NSString *subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
EDIT2:
Edit3:
Here´s the implementation:
#import "MapPin.h"
#implementation MapPin
#synthesize title, coordinate, subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
[super init];
subtitle = ttl;
title = #"Rating:";
coordinate = c2d;
return self;
}
- (void)dealloc {
[title release];
[subtitle release];
[super dealloc];
}
#end

You should send retain or copy message for object in init.
if (self = [super init]) {
subtitle = [ttl copy];
title = [#"Rating:" retain]
...
}
return self;
#"Rating:" is equal method which create autoreleased object.
Or you can use self.title = #"Rating:";.

Related

Object cannot be nil When trying to addObject

[__NSArrayM insertObject:atIndex:]: object cannot be nil
The problem is when trying to execute [_eventsArray addObject:eventModel]; error is occurred.
eventModel is nil.
How can I solve this problem ?
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) NSMutableArray *eventsArray;
#end
#interface EventsModel : NSObject
#property(nonatomic, assign) NSString *title;
#property(nonatomic, strong) NSString *details;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
EventsModel *eventModel ;
eventModel.title = #"Meeting";
eventModel.details = #"some description";
[_eventsArray addObject:eventModel];
}
#end
#pragma mark - Model Class
#implementation EventsModel
-(instancetype)init{
self = [super init];
if (self) {
self.title = nil;
self.details =nil;
}
return self;
}
#end
EventsModel *model = [EventsModel new];
model.title = #"test";
model.details = #"Detail test";
EventsModel *model2 = [EventsModel new];
model2.title = #"test2";
model2.details = #"Detail test2";
[_eventsArray addObject:model];
[_eventsArray addObject:model2];
NSLog(#"%lu elements in array",(unsigned long)_eventsArray.count);
you use EventsModel *model = [EvenntsModel new]; and create object
try this code

Multiple Pin subtitle annotations in MapKit?

I am parsing data from a csv file with coordinates and other information. I have the pins plotted and I would like to know how to add other pieces of data in the subtitle object of the pins. Here is my approach (the object I'm trying to add is "temperature"):
ViewController.h
NSString * latitude = [infos objectAtIndex:1];
NSString * longitude = [infos objectAtIndex:2];
NSString * description =[infos objectAtIndex:3];
NSString * address = [infos objectAtIndex:4];
NSString * temperature = [infos objectAtIndex:5];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
Location *annotation = [[Location alloc] initWithName:description address:address temperature:temperature coordinate:coordinate] ;
[mapview addAnnotation:annotation];
Location.h
#interface Location : NSObject {
NSString *_name;
NSString *_address;
NSString *_temperature;
CLLocationCoordinate2D _coordinate;
}
#property (copy) NSString *name;
#property (copy) NSString *address;
#property (copy) NSString *temperature;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address temperature:
(NSString*)temperature coordinate:(CLLocationCoordinate2D)coordinate;
Location.m
#implementation Location
#synthesize name = _name;
#synthesize address = _address;
#synthesize coordinate = _coordinate;
#synthesize temperature = _temperature;
- (id)initWithName:(NSString*)name address:(NSString*)address temperature:(NSString*)temperature coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_temperature = [temperature copy];
_coordinate = coordinate;
}
return self;
}
- (NSString *)title {
if ([_name isKindOfClass:[NSNull class]])
return #"Unknown charge";
else
return _name;
}
- (NSString *)subtitle {
return _address;
return _temperature;
}
As you can see I tried to add the "temperature" object in the "subtitle" method, unfortunately that didn't display in the annotation. Right now I am trying with the temperature object but I need to plot some more so I need a way to add as many objects as possible.
In the subtitle method:
- (NSString *)subtitle {
return _address;
return _temperature;
}
nothing after the return _address; line will execute.
That's how a return statement works (execution returns immediately to the caller).
Don't confuse the arrangement of the code with how the variables mentioned are displayed in the user interface.
Additionally, the built-in callout in MapKit only supports one short line each for the title and subtitle. Trying to display more than one line in each property will lead to disappointment.
So you could do:
- (NSString *)subtitle {
return _address;
}
or:
- (NSString *)subtitle {
return _temperature;
}
or:
- (NSString *)subtitle {
//display "address, temperature"...
return [NSString stringWithFormat:#"%#, %#", _address, _temperature];
}
If you must display a callout with a lot more detail than the default, you'll need to code a custom callout view which can get complex. See Custom MKAnnotation callout bubble with button and Show custom and default callout views on different pins with pin clustering on map view for a couple of examples to start with if interested.
By the way, unrelated but, the Location class should declare that it implements the MKAnnotation protocol. This will help you and the compiler avoid or display appropriate warnings and self-documents the code as a side benefit:
#interface Location : NSObject<MKAnnotation> {

override MKPointAnnotation class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I override MKAnnotation and add two property (NSManagedObject and UIImage) to MKAnnotation class something like that but it's not work what is wrong with this code?
// myAnnotation.h
#import <MapKit/MapKit.h>
#interface myAnnotation : NSObject<MKAnnotation>{
NSManagedObject *Contact;
UIImage *image;
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic,copy) NSManagedObject *Contact;
#property (nonatomic, copy) UIImage *image;
#property (nonatomic,assign) CLLocationCoordinate2D coordinate;
#property (nonatomic,copy) NSString *title;
#property (nonatomic,copy) NSString *subtitle;
#end
// myAnnotation.m
#import "myAnnotation.h"
#implementation myAnnotation
#synthesize Contact,title,subtitle,coordinate,image;
#end
// MapVC.h
#import "myAnnotation.h"
#interface MapVC : UIViewController<MKMapViewDelegate,UIActionSheetDelegate,MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate,UISearchBarDelegate>{
MKMapView *mapview;
myAnnotation *tmpContact;
}
// MapVC.m
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[mapview removeAnnotations:mapview.annotations];
//add each object in Contacts entity to map view
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest=[[NSFetchRequest alloc]initWithEntityName:#"Contacts"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)]];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];
for (NSManagedObject *info in fetchedObjects) {
NSLog(#"Name: %#", [info valueForKey:#"name"]);
//initializetion latitude and longitude
aLng=[[info valueForKey:#"lng"] doubleValue];
aLat=[[info valueForKey:#"lat"] doubleValue];
//if latitude and longitude not null
if(aLng && aLat && aLng!=0.0 &&aLat!=0.0)
{
//create a new Coordinate
CLLocationCoordinate2D wimLocation;
wimLocation.latitude=aLat;
wimLocation.longitude=aLng;
myAnnotation *myAnn=[myAnnotation alloc];
myAnn.coordinate=wimLocation;
myAnn.title=[info valueForKey:#"name"];
myAnn.Contact=info;
UIImage *image = [UIImage imageWithData:[info valueForKey:#"photo"]];
myAnn.image=image;
//add create Annotation to mapview
[self.mapview addAnnotation:myAnn];
}
}
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[myAnnotation class]])
{
static NSString *reuseId = #"ann";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
}
else
{
av.annotation = annotation;
}
myAnnotation *ann = (myAnnotation *)annotation;
av.image = ann.image;
return av;
}
//return nil (default view) if annotation is not our custom type
return nil;
}
Just subclass MKPointAnnotation or implement the MKAnnotation protocol in your custom class.

When I click the Pin not get the correct data. Solution?

I have a problem when I click on a pin are not sent the correct data. From what I understand even though it has a NSUInteger, these are always changing. How can I fix?
The code is:
DisplayMap.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#import <Parse/Parse.h>
#interface DisplayMap : NSObject <MKAnnotation>
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, copy) NSString *icon;
#property (nonatomic, strong) NSString *Telefono;
#property (nonatomic, strong) NSString *Email;
#property (nonatomic, strong) NSString *Sito;
#property (nonatomic, strong) NSString *TipologiaLocale;
#property (nonatomic, strong) NSString *Cucina;
#property (nonatomic, strong) NSString *Vegano;
#property (nonatomic, strong) NSString *Valutazione;
#property (nonatomic, strong) NSString *Latitudine;
#property (nonatomic, strong) NSString *Longitudine;
#property (nonatomic, strong) NSString *FaceB;
#property (nonatomic, strong) NSString *Twit;
#property (nonatomic, strong) PFFile *Anteprima1;
#property (nonatomic, strong) PFFile *Anteprima2;
#property (nonatomic, strong) PFFile *Anteprima3;
#end
DisplayMap.m
#import "DisplayMap.h"
#implementation DisplayMap
#synthesize coordinate;
#synthesize title;
#synthesize subtitle;
#synthesize icon;
#end
Alberghi.h
#import <UIKit/UIKit.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <MapKit/MapKit.h>
#import <Parse/Parse.h>
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#import "Reachability.h"
#import "TestFlight.h"
#import "MBProgressHUD.h"
#import "DisplayMap.h"
#import "Recipe.h"
#import "DettagliAlberghi.h"
#interface Alberghi : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {
MKMapView *_mapView;
}
#property (strong, nonatomic) IBOutlet CLLocationManager *locationManager;
#property (strong, nonatomic) IBOutlet CLGeocoder *geoCoder;
- (IBAction)TornaHome:(id)sender;
#end
Alberghi.m
#import "Alberghi.h"
#interface Alberghi ()
#end
#implementation Alberghi
#synthesize locationManager;
#synthesize geoCoder;
- (void)viewDidLoad {
[super viewDidLoad];
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 65, 320, 415)];
[_mapView setDelegate:self];
_mapView.layer.cornerRadius = 5;
locationManager.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
if(IS_OS_8_OR_LATER) {
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
_mapView.showsUserLocation = NO;
[_mapView setMapType:MKMapTypeStandard];
[_mapView setZoomEnabled:YES];
[_mapView setScrollEnabled:YES];
[self.view addSubview:_mapView];
PFQuery *query = [PFQuery queryWithClassName:#"Ristoranti"];
[query orderByDescending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
//NSLog(#"QUERY -----> :%#", objects);
for(NSDictionary *note1 in objects) {
float realLatitude1 = [[note1 objectForKey:#"Latitudine"] floatValue];
float realLongitude1 = [[note1 objectForKey:#"Longitudine"] floatValue];
NSLog(#"(PARSE) Latitudine: %f", realLatitude1);
NSLog(#"(PARSE) Longitudine: %f", realLongitude1);
DisplayMap *displayMap = [[DisplayMap alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude1;
theCoordinate.longitude = realLongitude1;
displayMap.coordinate = theCoordinate;
displayMap.title = [note1 objectForKey:#"NomeLocale"];
displayMap.subtitle = [note1 objectForKey:#"Indirizzo"];
displayMap.icon = [note1 objectForKey:#"PinMappa"];
[_mapView setDelegate:self];
[_mapView addAnnotation:displayMap];
}
}
}];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[DisplayMap class]])
{
return nil;
}
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (pinView == nil)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
}
pinView.canShowCallout = YES;
pinView.annotation = annotation;
DisplayMap *myAnn = (DisplayMap *)annotation;
pinView.image = [UIImage imageNamed:myAnn.icon];
// Create a UIButton object to add on the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[pinView setRightCalloutAccessoryView:rightButton];
return pinView;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
{
[self performSegueWithIdentifier:#"Prova" sender:view];
}
}
#pragma mark - mappa
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *annotationView in views) {
if (annotationView.annotation == mapView.userLocation) {
MKCoordinateSpan span = MKCoordinateSpanMake(1.4, 1.4);
MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span);
[mapView setRegion:region animated:YES];
}
}
}
- (IBAction)TornaHome:(id)sender
{
[self performSegueWithIdentifier:#"TornaHome" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Prova"]) {
MKAnnotationView *annotationView = sender;
DettagliAlberghi *destViewController =(DettagliAlberghi *) segue.destinationViewController;
DisplayMap *displayMap = (DisplayMap *)annotationView.annotation;
NSLog(#"DisplayMap? %#", displayMap);
NSLog(#"Dettagli Ristorante:\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#
\n%#", displayMap.title, displayMap.subtitle, displayMap.Telefono, displayMap.Email,
displayMap.Sito, displayMap.TipologiaLocale, displayMap.Cucina, displayMap.Vegano,
displayMap.Valutazione, displayMap.Latitudine, displayMap.Longitudine,
displayMap.Anteprima1, displayMap.Anteprima2, displayMap.Anteprima3, displayMap.FaceB,
displayMap.Twit);
destViewController.recipe = displayMap;
/*
MKAnnotationView *annotationView = sender;
NSLog(#"Esporto i dati: %#", annotationView);
DettagliAlberghi *destViewController = segue.destinationViewController;
DisplayMap *displayMap = [[DisplayMap alloc] init];
displayMap.title = [sender objectForKey:#"NomeLocale"];
displayMap.subtitle = [sender objectForKey:#"Indirizzo"];
displayMap.icon = [sender objectForKey:#"PinMappa"];
displayMap.Telefono = [sender objectForKey:#"Telefono"];
displayMap.Email = [sender objectForKey:#"Email"];
displayMap.Sito = [sender objectForKey:#"Sito"];
displayMap.TipologiaLocale = [sender objectForKey:#"TipologiaLocale"];
displayMap.Cucina = [sender objectForKey:#"Cucina"];
displayMap.Vegano = [sender objectForKey:#"Vegano"];
displayMap.Valutazione = [sender objectForKey:#"Valutazione"];
displayMap.Latitudine = [sender objectForKey:#"Latitudine"];
displayMap.Longitudine = [sender objectForKey:#"Longitudine"];
displayMap.Anteprima1 = [sender objectForKey:#"Anteprima1"];
displayMap.Anteprima2 = [sender objectForKey:#"Anteprima2"];
displayMap.Anteprima3 = [sender objectForKey:#"Anteprima3"];
displayMap.FaceB = [sender objectForKey:#"Facebook"];
displayMap.Twit = [sender objectForKey:#"Twitter"];
NSLog(#"Dettagli Ristorante: \n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#\n%#", displayMap.title, displayMap.subtitle, displayMap.Telefono, displayMap.Email, displayMap.Sito, displayMap.TipologiaLocale, displayMap.Cucina, displayMap.Vegano, displayMap.Valutazione, displayMap.Latitudine, displayMap.Longitudine, displayMap.Anteprima1, displayMap.Anteprima2, displayMap.Anteprima3, displayMap.FaceB, displayMap.Twit);
destViewController.recipe = displayMap;
*/
}
}
#end
When I click on the pin, the app crashes. I added an NSLog in PrepareToSegue and I noticed that all the fields to export in View next are empty, however, the data is read through PFQuery in viewDidLoad. What's wrong? When I click the pin does not export the data required?
2014-11-29 08:08:16.190 Veg[1083:8473] (PARSE) Latitudine: 45.435745
2014-11-29 08:08:16.190 Veg[1083:8473] (PARSE) Longitudine: 10.986951
2014-11-29 08:08:16.191 Veg[1083:8473] (PARSE) Latitudine: 45.441578
2014-11-29 08:08:16.191 Veg[1083:8473] (PARSE) Longitudine: 10.982130
2014-11-29 08:08:29.102 Veg[1083:8473] DisplayMap? <DisplayMap: 0x7f90335b71f0>
2014-11-29 08:08:29.102 Veg[1083:8473] Dettagli Ristorante:
La Lanterna
Piazzetta Portichetti, 6 Verona
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
(null)
2014-11-29 08:08:29.301 Veg[1083:8473] Double Coord: 0.000000, 0.000000
2014-11-29 08:08:31.356 Veg[1083:8473] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
I can not understand how to order ... always in the same order every time you open the View are always changing position and consequently when I click on a Pin View opens me different and not the one you want. Solutions? Thanks in advance
Update, add this in your viewDidLoad (in ViewController):
displayMap.icon = [note1 objectForKey:#"PinMappa"];
// Add this
displayMap.Telefono = [note1 objectForKey:#"Telefono"];
displayMap.Email = [note1 objectForKey:#"Email"];
displayMap.Sito = [note1 objectForKey:#"Sito"];
displayMap.FaceB = [note1 objectForKey:#"Facebook"];
displayMap.Twit = [note1 objectForKey:#"Twitter"];
displayMap.Cucina = [note1 objectForKey:#"Cucina"];
displayMap.TipologiaLocale = [note1 objectForKey:#"TipologiaLocale"];
displayMap.Valutazione = [note1 objectForKey:#"Valutazione"];
displayMap.Vegano = [note1 objectForKey:#"Vegano"];
// End of adding
[_mapView setDelegate:self];
Hi I assume you are using ARC, I'm afraid you have copied your code from a very old font, In fact a couple of them. (On example you have three instances variable what's name is title, one for the Protocol, and a couple of them on created an other from the property, with wrong synthesised). And the dealloc method better don't speak.
Well change your code to this: (Don´t worry It has the same variable you need (without duplicates).
DispalyMap.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <Parse/Parse.h>
#interface DisplayMap : NSObject <MKAnnotation>
#property (nonatomic, copy) NSString *icon;
#property (nonatomic, strong) NSString *Telefono;
#property (nonatomic, strong) NSString *Email;
#property (nonatomic, strong) NSString *Sito;
#property (nonatomic, strong) NSString *TipologiaLocale;
#property (nonatomic, strong) NSString *Cucina;
#property (nonatomic, strong) NSString *Vegano;
#property (nonatomic, strong) NSString *Valutazione;
#property (nonatomic, strong) NSString *Latitudine;
#property (nonatomic, strong) NSString *Longitudine;
#property (nonatomic, strong) NSString *FaceB;
#property (nonatomic, strong) NSString *Twit;
#property (nonatomic, strong) PFFile *Anteprima1;
#property (nonatomic, strong) PFFile *Anteprima2;
#property (nonatomic, strong) PFFile *Anteprima3;
#end
Display.m: (Only this)
#implementation DisplayMap
#synthesize coordinate = _coordinate;
#synthesize title = _title;
#synthesize subtitle = _subtitle;
#end
Well, I need to help you the crash message (because If with this change your App don't work, probably the mistake could be in the new view controller).
// Old answer:
I can't understand your code 100% because there are some code missing, but I think you made lot´s of thing you don't need. Try this methods, (Comment your current methods, and paste this).
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[DisplayMap class]])
{
return nil;
}
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (pinView == nil)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
}
pinView.canShowCallout = YES;
pinView.annotation = annotation;
DisplayMap *myAnn = (DisplayMap *)annotation;
pinView.image = [UIImage imageNamed:myAnn.icon];
// Create a UIButton object to add on the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[pinView setRightCalloutAccessoryView:rightButton];
return pinView;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure)
{
[self performSegueWithIdentifier:#"Prova" sender:view];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Prova"]) {
MKAnnotationView *annotationView = sender;
DettagliAlberghi *destViewController =(DettagliAlberghi *) segue.destinationViewController;
DisplayMap *displayMap = (DisplayMap *)annotationView.annotation;
destViewController.recipe = displayMap;
}
}

MKAnnotation not displaying on map view

I am trying to create a custom annotation for a simple map view of mine, but the annotaiton pin is not showing up on the map when i run the simulator.
I have a MapViewAnnotation class which implements the MKAnnotation protocol, and I have another MapViewController class where I programmatically create a mapView and display the map.
MapViewAnnotation.h :
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation>
#property (nonatomic, copy) NSString *title;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
-(id) initWithTitle: (NSString *) title AndCoordinate: (CLLocationCoordinate2D) coordinate;
#end
MapViewAnnotation.m :
#import "MapViewAnnotation.h"
#implementation MapViewAnnotation
#synthesize coordinate = _coordinate;
#synthesize title = _title;
-(id) initWithTitle:(NSString *)title AndCoordinate:(CLLocationCoordinate2D)coordinate{
self = [super init];
_title = title;
_coordinate = coordinate;
return self;
}
#end
MapViewController.h :
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MapViewController : UIViewController
#property (weak, nonatomic) MKMapView *mapView;
-(void) goToLocation;
#end
MapViewController.m :
#import "MapViewController.h"
#import "MapViewAnnotation.h"
#define LATITUDE 42.3889;
#define LONGITUDE -72.5278;
#interface MapViewController ()
#end
#implementation MapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.mapView];
[self goToLocation];
[self.mapView addAnnotations:[self createAnnotations]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) goToLocation {
MKCoordinateRegion newRegion;
newRegion.center.latitude = LATITUDE;
newRegion.center.longitude = LONGITUDE;
newRegion.span.latitudeDelta = 0.05f;
newRegion.span.longitudeDelta = 0.05f;
self.mapView.showsPointsOfInterest = YES;
[self.mapView setRegion:newRegion animated:YES];
}
-(NSMutableArray *) createAnnotations {
NSMutableArray *annotations = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:#"location" ofType:#"plist"];
NSArray *locations = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *row in locations){
NSNumber *latitude = [row objectForKey:#"latitude"];
NSNumber *longitude = [row objectForKey:#"longitude"];
NSString *title = [row objectForKey:#"title"];
CLLocationCoordinate2D coord;
coord.latitude = latitude.doubleValue;
coord.longitude = longitude.doubleValue;
MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord];
[annotations addObject: annotation];
}
return annotations;
}
#end
I am taking am going through a plist where I have coordinates for one test location that I am trying to show on my map, but the pin is not displaying on the map. Again, I am doing this all programmatically, no xib, or storyboards (although that should not be the problem).
I do not know why the annotation is not showing up. I tried looking on stack for some other situations like mine but was unlucky to find anything that could help.
Thank you.
Answer was pretty simple,
In my plist I had the coordinates
latitude: 42.37
longitude: 72.536
when it should of actually been -72.536 for longitude.

Resources