I have a mapview where i update my currentlocation with CoreLocation, but I also have checks which uses userLocation.
I still haven't found an alternative in fixing my problem
But for some reason I can't use userLocationVisible to hide the blue dot.
When I enter my MapView I start the locationManager, but before I have updated my location, the blue dot appears and my pin doesn't show up.
I've tried to use a custom MKAnnotation and init the coordinates with the newLocation from DidUpdateToLocation. But when I run this I get:
-[CustomPlacemark setCoordinate:]: unrecognized selector sent to instance 0x1de6c0
this is my CustomPlacemark:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface CustomPlacemark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;
#end
#import "CustomPlacemark.h"
#implementation CustomPlacemark
#synthesize coordinate;
#synthesize title, subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
self=[super init];
if (self!=nil) {
coordinate=c;
}
return self;
}
-(NSString*)title{
return title;
}
-(NSString*)subtitle{
return subtitle;
}
-(void) dealloc
{
[title release]; title = nil;
[subtitle release]; subtitle = nil;
[super dealloc];
}
#end
Can someone also tell me why I can't use UserLocationVisible??
cordinate is read only property for customplacemark class . so u can not set cordinate property. to set cordinate property make it read and write.
change line#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
To #property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
The right way to do it will be initWithCoordinate rather than making the property readwrite
The blue dot appears when you set showsUserLocation=YES; If you want to hide the blue dot, set this to NO. Alternatively, you can determine if the user's location is visible on the screen using CoreLocation and enable showsUserLocation to YES.
Also, the blue dot is a special annotation class MKUserLocation that conforms to MKAnnotation protocol. If you are sending any messages to annotation objects that are your own class, you may want to exclude MKUserLocation annotation object.
If you want to know how to specifically deal with your custom annotation objects without sending incorrect messages to MKUserLocation class, let me know, I have the code that I can send.
Related
I'm trying to make a custom MKAnnotation class so it can hold additional information but i keep getting this error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [AnnotationForId setCoordinate:]: unrecognized selector sent to instance 0x16f63340'
I've tried looking up how to create one and i've followed what i've found online and i'm confused at why i keep getting this error.
Here is my custom annotation class.
#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#interface AnnotationForId : NSObject <MKAnnotation >{
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
NSInteger shopId;
}
#property(nonatomic)NSInteger shopId;
#property (nonatomic, copy) NSString * title;
#property (nonatomic, copy) NSString * subtitle;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#end
I've synthesized the property variables in .m class.
I try to call the custom class in my main controller:
for(Shop *shops in feed.shops){
NSLog(#"reached");
AnnotationForId *shop = [[AnnotationForId alloc] init];
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
shop.coordinate = coords;
//[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
shop.title = shops.name;
shop.subtitle = #"Coffee Shop";
[map addAnnotation:shop];
}
Any help to why this isnt working would be awesome.
Thanks.
It seems like you're trying to set your readonly property.
You declared it as readonly:
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
But then you're trying to use setter:
shop.coordinate = coords;
readonly means that no setter will be defined for other classes to leverage.
Edit:
I think you should add convenience initializer to your AnnotationForId class like:
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord
{
self = [super init]; // assuming init is the designated initialiser of the super class
if (self)
{
coordinate = coord;
}
return self;
}
So your code will look like this:
for(Shop *shops in feed.shops){
NSLog(#"reached");
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
AnnotationForId *shop = [[AnnotationForId alloc] initWithCoordinate:coords];
//[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
shop.title = shops.name;
shop.subtitle = #"Coffee Shop";
[map addAnnotation:shop];
}
This is Simple Example For how to Create Custom AnnotationView.
Create custom AnnotationView:
#import <MapKit/MapKit.h>
#interface AnnotationView : MKPlacemark
#property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSString *subtitle;
// you can put here any controllers that you want. (such like UIImage, UIView,...etc)
#end
And in .m file
#import "AnnotationView.h"
#implementation AnnotationView
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
{
self.coordinate = coordinate;
}
return self;
}
#end
// Use Annotation Add #import "AnnotationView.h" in your relevant .m file:
CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;
// Create Obj Of AnnotationView class
AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;
annotation.title = #"I m Here";
annotation.subtitle = #"This is Sub Tiitle";
[self.mapView addAnnotation:annotation];
Above is simple Example of how to create AnnotationView.
You have given the property as readonly, which sets only a getter method for the property. So please make the property retain and try it once again.
I have a problem accessing a object in my array. I store "Place" objects in my NSMutableArray. I want to access this array for my TableView. I get the "No known instance method for selector" error in line one. See lines below.
cell.imageView = [[self.currentPlaces objectAtIndex:indexPath.row]picture];
cell.subtitleLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]description];
cell.objectNameLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]name];
This is my Place object:
#interface Place : NSObject{
CLLocation *objectLocation;
UIImageView *picture;
NSString *name;
NSString *description;
}
The access of the properties "description" and "name" is no problem. I just dont know why this error occurs.
Thx. Dominik
I had the same problem; what worked for me was passing the UIImage instead of the UIImageView. So your code should look like this:
#interface Place : NSObject{
CLLocation *objectLocation;
UIImage *picture;
NSString *name;
NSString *description;
}
and this
cell.imageView.image = [[self.currentPlaces objectAtIndex:indexPath.row]picture];
cell.subtitleLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]description];
cell.objectNameLB.text = [[self.currentPlaces objectAtIndex:indexPath.row]name];
If that doesn't work I'll post some more code for you to look at.
You haven't actually declared any methods. What you have declared are instance variables. You should probably be using #propertys instead.
#interface Place : NSObject
#property (nonatomic, retain) CLLocation *objectLocation;
#property (nonatomic, retain) UIImageView *picture;
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy, getter=objectDescription) NSString *description;
#end
This will actually create the methods that you want. Note that I changed the method for the description property to read -objectDescription. This is because NSObject already declares the -description method and you shouldn't be overriding it with an unrelated property.
If you're on recent Clang, then this is all you need, and instance variables will get synthesized automatically (using an underbar prefix, e.g. _picture). If you're on an older version (e.g. if this causes errors), you need to add #synthesize lines, as in
#implementation Place
#synthesize objectLocation=_objectLocation;
#synthesize picture=_picture;
#synthesize name=_name;
#synthesize description=_description;
#end
I have seen that a few others have had this problem as well.
I'm trying to follow a tutorial online that shows how to create animated pins on a MapView.
I have implemented the code as shown in the tutorial and the project builds fine except I receive this exception:
-[MKPointAnnotation iconN]: unrecognized selector sent to instance
I have a subclass of 'MKPinAnnotationView' and in the .m file I create this method:
- (void)setAnnotation:(id<MKAnnotation>)annotation {
[super setAnnotation:annotation];
//Place *place = [[Place alloc] init];
Place *place = (Place *)annotation;
//The following line is where the program sends "SIGABRT"
icon = [UIImage imageNamed:[NSString stringWithFormat:#"pin_%d.png", [place.iconN intValue]]];
[iconView setImage:icon];
}
Here are a few parts from my "model" which is called Place.h/.m.
Here is where I create the property for 'iconN'.
#property (retain, nonatomic) NSNumber *iconN;
And here I synthesize it:
#synthesize iconN = _iconN;
Any help is greatly appreciated.
EDIT:
Here is the Place.h and Place.m
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface Place : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
}
#property (retain, nonatomic) NSNumber *iconN;
#property (nonatomic, copy) NSString *title;
#property (nonatomic) CLLocationCoordinate2D coordinate;
- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber;
#end
And the Place.m
#import "Place.h"
#implementation Place
#synthesize coordinate;
#synthesize iconN = _iconN;
#synthesize title;
- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber {
self = [super init];
if (self) {
coordinate = CLLocationCoordinate2DMake(lat, lon);
self.iconN = iconNumber;
}
return self;
}
- (NSString *)title {
return [NSString stringWithFormat:#"Bus: %d", [self.iconN intValue]];
}
- (NSString *)subtitle {
return [NSString stringWithFormat:#"bus[%d] from database.", [self.iconN intValue] - 1];
}
#end
You cannot convert a MKAnnotation to a Place just by casting it. This line is wrong.
Place *place = (Place *)annotation;
You should post your Place.h and Place.m files if you're still stuck. You need to either set the iconN property on a new Place object, or create an init method in the Place class that accepts the MKAnnotation object as a parameter and sets it own internal values accordingly.
In the line
Place *place = (Place *)annotation;
has the variable place of annotation variable class (MKPointAnnotation), you are not able to bring the master class variable to a subclass in this way. Instead you'll have to make a constructor for Place from MKPointAnnotation and perform a check in the setAnnotation method that annotation is of MKPointAnnotation.
You are sending the message to the annotation but you seem to have subclasses the annotation view.
Posting as an answer what was originally just a comment:
I'm not familiar with the MapKit, but the thing that sticks out for me in this: -[MKPointAnnotation iconN]: unrecognized selector sent to instance is that the class is MKPointAnnotation. So the annotation you're receiving isn't actually a Place object, it's an MKPointAnnotation object - you can't just cast to Place. I suspect the root of your problem is where you create your annotation object in the first place.
My iPhone app is either crashing due to to a zombie, or leaking memory.. I've narrowed it down to 3 lines of code and can reliably get one of the two things to happen by commenting/uncommenting the code. The bugs occur when navigation between a list of results (tableView) and a details page containing a map and a few labels, memory leak happens the first time I navigation from the map back to the list of results, the zombie occurs after maybe 5/6 times navigating to different results and back.
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#define METERS_PER_MILE 1609.344
#interface ResDetailsPageVC : UIViewController <MKMapViewDelegate, UIAlertViewDelegate> {
UISegmentedControl *mapTypeSwitcher;
MKMapView *mapView;
UILabel *nameLabel;
UIButton *addressLabel;
UILabel *telephoneLabel;
NSString *address;
}
#property (nonatomic, retain) IBOutlet UISegmentedControl *mapTypeSwitcher;
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UIButton *addressLabel;
#property (nonatomic, retain) IBOutlet UILabel *telephoneLabel;
- (IBAction)segmentedControlIndexChanged;
- (IBAction)callButtonClick;
- (IBAction)addressClick;
- (void) callNumber;
#end
#synthesize mapView;
#synthesize mapTypeSwitcher;
#synthesize nameLabel, addressLabel, telephoneLabel;
- (void)dealloc {
// if these lines are commented out - memory leak
// if not - zombie?!
/*self.telephoneLabel = nil;
self.addressLabel = nil;
self.nameLabel = nil;*/
self.mapView = nil;
self.mapTypeSwitcher = nil;
[super dealloc];
}
Somewhere some other piece of code is using the same object whose address is stored in one of those three properties, but that other piece of code has not properly retained the object.
I recommend this to you:
- (void)dealloc {
[telephoneLabel release]; telephoneLabel = nil;
[addressLabel release]; addressLabel = nil;
....
[super dealloc];
}
I have a warning in my custom class for the MKAnnotation.
In iOS 5.0, apple add a new readonly property, the title in the MKAnnotation class, but I already have this property in my custom MKAnnotation.
Then, how can I set the title in the MKAnnotation?
Interest link: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html
And here is my code for iOS less than 5.0:
// in MyMKAnnotation.h
#interface MyMKAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, retain) NSString *title;
// in MyMKAnnotation.m
- (id) initWithTitle:(NSString *)_title:(NSString *)_title localizacion:(CLLocationCoordinate2D)_localizacion
{
coordinate = _localizacion;
title = _title; //-----------> here is taking the warning
return self;
}
Than you!! :)
With #Lefteris help, I write this code:
// if the iOS version is lesser than 5.0, its retain, otherwise its copy
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_5_0
#property (nonatomic, retain) NSString *title;
#else
#property (nonatomic, copy) NSString *title;
#endif
Thank you very much!! :)
BASED IN: Why after upgrading to Xcode 4.2 does MKAnnotation display a warning