Not able to initialize CLLocationCoordinate2D iVar in MKAnnotation Classe - ios

I am trying to put MKAnnotations on MKMapView. I am following the procedure that has been suggested on various available tutorials. I have created PlaceMark.h/.m file from NSobject Class as following.
PlaceMark.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface PlaceMark : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
}
#property (nonatomic,copy) NSString *title;
#property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
#end
PlaceMark.m
#import "PlaceMark.h"
#implementation PlaceMark
#synthesize coordinate,title;
-(void)dealloc
{
[title release];
[super dealloc];
}
#end
in my viewController which holds MKMapView, in viewDidload I have following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MKCoordinateRegion region;
region.center.latitude=37.3305262;
region.center.longitude=-122.0290935;
region.span.latitudeDelta=0.01;
region.span.longitudeDelta=0.01;
[parkingMap setRegion:region animated:YES];
PlaceMark *ann=[[[PlaceMark alloc]init]autorelease]; // I have also tired it using explicitly defining method -(id)initwithTitle.... but it did not worked.
ann.title=#"Test";
ann.coordinate= region.center;
[parkingMap addAnnotation:ann];
}
Can anyone please tell me what I am doing wrong? BTW I am using Xcode4.2 with iOS sdk5.0 and
Not using storyboards/automatic reference counting
Thanks
Sumit

You've defined the coordinate property as readonly so you can't set it.
Change it to readwrite.
Also, if you just need a basic annotation object with a title, subtitle, and settable coordinate, you can use the built-in MKPointAnnotation class instead of defining your own class. The creation and initialization would be identical (just replace your class name with MKPointAnnotation).

Related

The Pin is not working

My Map shows a pin on a specific place but it's not showing the pin
Here is the code
WadiRumViewControllerJordan.h
#import <UIKit/UIKit.h>
#include <MapKit/MapKit.h>
#interface WadiRumViewControllerJordan : UIViewController
#property (strong, nonatomic) IBOutlet MKMapView *WadiRumMapView;
#end
WadiRumViewControllerJordan.m
#import "WadiRumViewControllerJordan.h"
#import "WadiRumNSOjectPIN.h"
#interface WadiRumViewControllerJordan ()
#end
//Wadi Rum Coordinates
#define WadiRum_Latitude 29.537355
#define WidiRum_longtitude 35.415026
//Wadi Rum Span
#define WadiRumSpan 0.01f;
#implementation WadiRumViewControllerJordan
#synthesize WadiRumMapView;
- (void)viewDidLoad {
[super viewDidLoad];
//Create WadiRum Region
MKCoordinateRegion WadiRumRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = WadiRum_Latitude;
center.longitude = WidiRum_longtitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta = WadiRum_Latitude;
span.longitudeDelta = WidiRum_longtitude;
WadiRumRegion.center = center;
WadiRumRegion.span = span;
//Set our map
[WadiRumMapView setRegion:WadiRumRegion animated:YES];
//WadiRumNSObjectPIN
//1. Create a coordinate for the use of WadiRum
CLLocationCoordinate2D WadiRumLocation;
WadiRumLocation.latitude = WadiRum_Latitude;
WadiRumLocation.longitude = WidiRum_longtitude;
WadiRumNSOjectPIN * WadiRumAnnitation = [[WadiRumNSOjectPIN alloc] init];
WadiRumAnnitation.coordinate = WadiRumLocation;
WadiRumAnnitation.title = #"Services";
WadiRumAnnitation.subtitle = #"Desert";
{[self.WadiRumMapView addAnnotation:WadiRumAnnitation];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be WadiRumNSOjectPIN
}
#end
WadiRumNSOjectPIN.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface WadiRumNSOjectPIN : NSObject <MKAnnotation>
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
#property(nonatomic, copy) NSString * title;
#property(nonatomic, copy) NSString * subtitle;
#end
WadiRumNSOjectPIN.m
#import "WadiRumNSOjectPIN.h"
#implementation WadiRumNSOjectPIN
#synthesize coordinate;
- (id)initWithLocation:(CLLocationCoordinate2D)coord {
self = [super init];
if (self) {
coordinate = coord;
}
return self;
}
#synthesize coordinate, title, subtitle;
#end
I edited the code above to make it exactly like what I want, I got this error in the picture bellow
In order to conform to MKAnnotation, you must have properties called coordinate, title and subtitle. You've added three extra properties, ttcoordinate, tttitle, and ttsubtitle, but MKAnnotation is going to ignore those, and will look for coordinate, title, and subtitle.
The key reason you're not seeing your annotation is that you're setting ttcoordinate in viewDidLoad. But MKAnnotation will not use that, but rather will refer to the coordinate property you synthesized, but never set. (You do have an initWithLocation method, which suggests you were going to update coordinate, but you never call that.)
Bottom line, I would suggest renaming ttcoordinate, ttitle and ttsubtitle to coordinate, title, and subtitle, and updating all of those references accordingly, and that should fix everything. And you can retire the #synthesize line.

mapview autozoom on current location automatic

I have one problem. I've been looking at the other answers here on stack overflow about the same question and I can't get them to work for me. So I'm going to try asking here. What I want to do is when I'm on map view and have got the user location I want to automatically zoom into the location.
the h-file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface WalkingTableViewController: UIViewController <MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *MKMapView;
#end
the m-file
#import "WalkingTableViewController.h"
#interface UITableViewController ()
#end
#implementation WalkingTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.MKMapView.showsUserLocation=YES;
self.MKMapView.delegate = self;
[self.MKMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
- (IBAction)StopButton:(id)sender
- (IBAction)StartButton:(id)sender
#end
If you like to see the zoom animated, I would place the code in the viewDidAppear method, so that the animation starts once the view controller is displayed.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CLLocationManager* tempLocationManager = [[CLLocationManager alloc] init];
MKCoordinateRegion tRegion = MKCoordinateRegionMakeWithDistance([tempLocationManager.location coordinate], 20000, 20000);
[self.MKMapView setRegion:tRegion animated:animated];
}
Adjust the region (zoom level, 20000 in the example above) to your needs.
What you have to do is tell the mapview to zoom into a region. This should get you started:
CLLocationCoordinate2D center = _mapView.userLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMake(center, MKCoordinateSpanMake(2000, 2000));
[_mapView setRegion:region animated:YES];

class -(MyAnnotation annotation) was deallocated

An instance 0x11d0ce4b0 of class -(MyAnnotation annotation) was
deallocated while key value observers were still registered with it.
Observation info was leaked, and may even become mistakenly attached
to some other object. Set a breakpoint on NSKVODeallocateBreak to stop
here in the debugger. Here's the current observation info:
But I have put a breakpoint on my dealloc method, and in there, I do deregister for those notifications. Dealloc is called, and I did check that it was the same object, and all the calls to deregister are there. So I don't know how it could not be removing the observers.
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;
#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.

initialization and allocation without arc?

Sorry its a basic question, I want to know why my code doesn't need alloc/init for mapView. Does it happen automatically on retain ? I am not using ARC and on alloc/init of my MKMapView* mapView it does not result in error but the map view doesn't show the location information and also doesn't appear as hybrid type....but on removing the alloc/init statement fro viewDidLoad it works all fine !! why?
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface MDViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>
#property (retain, nonatomic) IBOutlet MKMapView* mapView;
#end
-----
#import "MDViewController.h"
#interface MDViewController ()
{
CLLocationManager* lmanager;
}
#end
#implementation MDViewController
#synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lmanager= [[CLLocationManager alloc]init];
lmanager.delegate=self;
lmanager.desiredAccuracy=kCLLocationAccuracyBest;
lmanager.distanceFilter=kCLDistanceFilterNone;
[lmanager startUpdatingLocation];
//mapView = [[MKMapView alloc]init];//without allocating here it works
mapView.delegate=self;
mapView.mapType=MKMapTypeHybrid;
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//update map
MKCoordinateSpan span;
span.latitudeDelta= .001;
span.longitudeDelta=.001;
MKCoordinateRegion region;
region.center= newLocation.coordinate;
region.span=span;
[mapView setRegion:region animated:YES];
[mapView setShowsUserLocation:YES];
}
You don't need to alloc init your mapview because it is done for you by the Xib. When loading the interface xib, the frameworks see you have a frozen mapview and alloc init it automagically, then assign that mapview to the one in your viewcontroller code.
If in your code you alloc init, you break the link between the two.
One way to make it work is to have no mapview in your IB xib and alloc init it, setDelegate, set frame and finally add its view as a subview of the main view.
I try to stay concise. I hope it us clear to you. And ARC has no relation whatsoever with all that.

Is it possible to inherit from MKPolyline

I'm building a MapKit based app for iPhone.
I have a number of MKPolylines added to the map.
However, instead of having a MKPolyline, I would like to have my own Model class conforming to the MKOverlay protocol added to the map so that I can access the model properties when creating the corresponding view in mapView:viewForOverlay.
The problem is that I can't find the way to inherit from MKPolyline because it doesn't have any init methods that I can call from the subclass' init. You can only create them using the convenience methods.
How can I bring together the model properties and the MKPolyline behaviour?
MANIAK_dobrii's code is the way to go but I found I had to implement some additional MKMultiPoint methods to get it to work, here are my complete header and implementation files for an AnchorLine class I used:-
Header AnchorLine.h
#import <MapKit/MapKit.h>
#interface AnchorLine : NSObject <MKOverlay> {
MKPolyline* polyline;
}
#property (nonatomic, retain) MKPolyline* polyline;
+ (AnchorLine*)initWithPolyline: (MKPolyline*) line;
#end
Implementation AnchorLine.m
#import "AnchorLine.h"
#implementation AnchorLine
#synthesize polyline;
+ (AnchorLine*)initWithPolyline: (MKPolyline*) line {
AnchorLine* anchorLine = [[AnchorLine alloc] init];
anchorLine.polyline = line;
return [anchorLine autorelease];
}
- (void) dealloc {
[polyline release];
polyline = nil;
[super dealloc];
}
#pragma mark MKOverlay
//#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (CLLocationCoordinate2D) coordinate {
return [polyline coordinate];
}
//#property (nonatomic, readonly) MKMapRect boundingMapRect;
- (MKMapRect) boundingMapRect {
return [polyline boundingMapRect];
}
- (BOOL)intersectsMapRect:(MKMapRect)mapRect {
return [polyline intersectsMapRect:mapRect];
}
- (MKMapPoint *) points {
return [polyline points];
}
-(NSUInteger) pointCount {
return [polyline pointCount];
}
- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range {
return [polyline getCoordinates:coords range:range];
}
#end
Hope that helps someone.
You can set an associated object attribute of the class. This allows you to bind an instance variable to an existing class. Make sure you properly clean up after yourself.
It is true that MKPolyline doesn't have its own init method. In fact the only class in MKPolyline's inheritance chain that does have an init method is NSObject.
So when I subclassed MKPolyline I just overrode the init method defined by NSObject...
-(id) init {
self = [super init];
if(self) {
//my initialization here
}
return self;
}
Then when you want to instantiate your subclass with coordinates you might do something like this...
-MyPolyline* myPolyline = (MyPolyline*)[MyPolyline polylineWithCoordinates:coordinates count:coordinateCount];
UPDATE: There's another option (could be better) to use message forwarding for this (like -forwardingTargetForSelector or stuff).
I had the same issue today but came up with other solution. Instead of using suggested by Wayne associated object attribute stuff I just encapsulated MKPolyline in another class and transferred MKOverlay protocol's messages to it.
So I've got something like in .h:
#interface MyOverlay : NSObject <MKOverlay>
{
MKPolyline* polyline;
id object;
}
#property (nonatomic, retain) id object;
#property (nonatomic, retain) MKPolyline* polyline;
+ (MyOverlay*)myOverlayWithObject: (id)anObject;
#end
And in .m:
#implementation MyOverlay
#synthesize object;
#synthesize polyline;
+ (MyOverlay*)routePartOverlayWithObject: (id)anObject {
MyOverlay* myOverlay = [[MyOverlay alloc] init];
... generating MKPolyline ...
myOverlay.polyline = ... generated polyline ...;
routePartOverlay.object = anObject;
return [myOverlay autorelease];
}
- (void) dealloc {
[cdRoutePart release]; cdRoutePart = nil;
[polyline release]; polyline = nil;
[super dealloc];
}
#pragma mark MKOverlay
//#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (CLLocationCoordinate2D) coordinate {
return [polyline coordinate];
}
//#property (nonatomic, readonly) MKMapRect boundingMapRect;
- (MKMapRect) boundingMapRect {
return [polyline boundingMapRect];
}
- (BOOL)intersectsMapRect:(MKMapRect)mapRect {
return [polyline intersectsMapRect:mapRect];
}
#end
So MyOverlay behaves like MKPolyline (conforms to MKOverlay) and at the same time I can do anything with it, having as many properties as I need.
I rewrote jmathew's excellent answer which I didn't find anywhere else, and it works like charm. A little modified version in Swift:
final class CustomPolyline: MKPolyline {
private(set) var color: UIColor?
private(set) var width: CGFloat?
convenience init(coordinates: [CLLocationCoordinate2D], color: UIColor, width: CGFloat) {
self.init(coordinates: coordinates, count: coordinates.count)
self.color = color
self.width = width
}
}
Whats mentioned here so far hasn't quite worked for me but I managed a solution based on the other answers and some independent research. I am not 100% certain in this but you can cast a MKPolyline into a custom sub-class only if you use the static method call that calls the right 'init' method internally.
(CustomPolyline*)[CustomPolyline polylineWithCoordinates:coordinates count:coordinateCount]
The above won't work because polylineWithCoordinates only allocates memory for an MKPolyline object and not CustomPolyline. I suspect what's happening internally is that polylineWithCoordinates calls another initializer method in a manner similar to: [MKPolyline otherInitMethod:...]. And its not allocating the proper amount of memory because its now using an MKPolyline static method call and not our CustomPolyline static call.
However if we use
(CustomPolyline*)[CustomPolyline polylineWithPoints:polyline.points count:polyline.pointCount];
It does work. I think this is because polylineWithPoints is using an initializer that returns an id not just chaining to another method call. And since we called it using the CustomPolyline class the initializer allocates memory for CustomPolyline not MKPolyline.
I could be completely wrong on why it works. But I've tested this and it seems to work fine. MKPolygon can be extended in a similar manner. In that case I think the correct static method to use is MKPolygon polygonWithCoordinates:points count:pointSet.count]]
My implementation for reference:
CustomPolyline.h
#import <MapKit/MapKit.h>
typedef enum {
CustomPolylineTypeNone = 0,
CustomPolylineDifferentStrokes
} CustomPolylineType;
/**
* CustomPolyline wraps MKPolyline with additional information about a polyline useful for differentiation.
*/
#interface CustomPolyline : MKPolyline
#property CustomPolylineType type;
-(CustomPolyline*)initWithMKPolyline:(MKPolyline*)polyline;
#end
CustomPolyline.m
#import "CustomPolyline.h"
#implementation CustomPolyline
#synthesize type;
/**
* Takes an MKPolyline and uses its attributes to create a new CustomPolyline
*/
-(CustomPolyline*)initWithMKPolyline:(MKPolyline*)polyline
{
// We must use the this specific class function in this manner to generate an actual
// CustomPolyline object as opposed to a MKPolyline by a different name
return (CustomPolyline*)[CustomPolyline polylineWithPoints:polyline.points count:polyline.pointCount];
}
#end

Resources