Adding more annotations to UIMapView - objective-c

I want to add more annotations to my mapview. I'm working with a class Annotation where there is a method addAnnotation. This method is being called in my UIViewController when I want to add a new annotation.
But for some reason, only the annotation I added last, is displayed.
Annotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface Annotation : NSObject {
CLLocationCoordinate2D cooridnate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, retain) NSMutableArray *locations;
-(void)addAnnotation: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate;
#end
Annotation.m
#import "Annotation.h"
#implementation Annotation
#synthesize coordinate, title, subtitle, locations;
-(void)addAnnotation: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate {
locations = [[NSMutableArray alloc] init];
self.coordinate = initCoordinate;
self.title = [NSString stringWithFormat:#"%#", initTitle];
self.subtitle = [NSString stringWithFormat:#"%#", initSubtitle];
[locations addObject:self]; // add all my anotations with location to an array
}
#end
Method in my UIViewController
#import "MapViewController.h"
#interface MapViewController ()
#end
#implementation MapViewController
#synthesize mapView;
#pragma mark - ViewController methods
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initMapView];
ann = [[Annotation alloc] init]; // making place in memory
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIMapController methods
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
[self createAnnotations];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
//If annotation = user position ==> DON'T CHANGE
return nil;
}
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorRed;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = NO;
MyPin.animatesDrop= FALSE;
MyPin.canShowCallout = YES;
return MyPin;
}
#pragma mark - MY Methods
-(void) initMapView {
[mapView setMapType:MKMapTypeStandard]; // standaard maptype
[mapView setScrollEnabled:YES];
[mapView setZoomEnabled:YES];
[mapView setDelegate:self]; // gegevens van de map terugsturen naar deze viewController en de gebruiker
[mapView setShowsUserLocation:YES];
[self focusWorld];
}
-(void) createAnnotations {
[self initSomeExamples];
[self.mapView addAnnotations: ann.locations];
}
-(void) focusWorld {
MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld); // regio instellen op World
mapView.region = worldRegion; // regio doorgeven naar onze mapView
}
-(void) initSomeExamples {
// Washington
l1.latitude = 38.892102;
l1.longitude = -77.029953;
// Antwerp
l2.latitude = 51.219787;
l2.longitude = 4.411011;
// Egypt
l3.latitude = 29.993002;
l3.longitude = 31.157227;
// Brazil
l4.latitude = -22.900846;
l4.longitude = -43.212662;
[ann addAnnotation:#"America has a new President !" :#"Washington DC" :l1]; // call method to add an annotation with these parameters
[ann addAnnotation:#"Revolutionary app by Belgium student" :#"Antwerp" :l2];
[ann addAnnotation:#"The new Arabic revolution" :#"Egypt, Tahir square" :l3];
[ann addAnnotation:#"World Championchip football" :#"Rio de Janeiro" :l4];
}
#pragma mark - ACTIONS
-(void)showInfo:(id)sender {
NSLog(#"Show info");
}
#end

You don't need locations #property and - addAnnotation: method. One MKAnnotation object represents a location.
My suggestion is to modify Annotation class accordingly, as well as conforming and create it's objects as many as number of locations you would like to annotate.
Remove locations #property and -addAnnotation: method from Annotation.h/m
Annotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface Annotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
-(id) initWithTitle: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate;
#end
Annotation.m
#import "Annotation.h"
#implementation Annotation
#synthesize coordinate, title, subtitle;
-(id) initWithTitle: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate {
self = [super init];
if ( self ) {
self.coordinate = initCoordinate;
self.title = [NSString stringWithFormat:#"%#", initTitle];
self.subtitle = [NSString stringWithFormat:#"%#", initSubtitle];
}
return self; // correction appreciated.
}
#end
Some methods changed in your view controller
-(void) initSomeExamples {
// Washington
l1.latitude = 38.892102;
l1.longitude = -77.029953;
// Antwerp
l2.latitude = 51.219787;
l2.longitude = 4.411011;
// Egypt
l3.latitude = 29.993002;
l3.longitude = 31.157227;
// Brazil
l4.latitude = -22.900846;
l4.longitude = -43.212662;
NSArray *annotations = #[
[[Annotation alloc] initWithTitle:#"America has a new President !" :#"Washington DC" :l1],
[[Annotation alloc] initWithTitle:#"Revolutionary app by Belgium student" :#"Antwerp" :l2],
[[Annotation alloc] initWithTitle:#"The new Arabic revolution" :#"Egypt, Tahir square" :l3],
[[Annotation alloc] initWithTitle:#"World Championchip football" :#"Rio de Janeiro" :l4] ];
// Adding 4 annotation objects
[self.mapView addAnnotations:annotations];
}
-(void) createAnnotations {
[self initSomeExamples];
// Annotations are added in -initSomeExamples method.
}

Related

Problems with Location Manager

I've been stuck on this problem for ages and I'm sure the reason is that I don't know enough about objective-c. I've only just started using coding with it!
Anyway I'm trying to design an app that tracks the current distance travelled and also displays the route that have been taken using MKPolyLine. I've got MKPolyLine sorted now but the problem I;m having is with getting the current distance. I think the code is OK but I think i'm doing something funny with location manager.
I've done a bit of debugging and found that my second locationManager method does not get called.
Here is the code below, if someone can take a quick look and just point me in the right direct it would be greatly appreciated.
Many Thanks.
.m :
#interface StartCycleViewController ()
#property (nonatomic, strong) CLLocationManager *locationManager;
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property (nonatomic, strong) UIView *containerView;
#property (nonatomic, strong) CLLocationManager *distanceManager;
#end
#implementation StartCycleViewController
static double totalDistanceBetween;
#synthesize cycleLocation = _cycleLocation;
#synthesize currentCycleLocation = _currentCycleLocation;
#synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self startCycleLocation];
[mapView setDelegate:self];
self.locations = [NSMutableArray array];
}
- (void)dealloc
{
self.locationManager.delegate = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - startCycleLocation
- (void)startCycleLocation{
if (!_cycleLocation){
_cycleLocation = [[CLLocationManager alloc]init];
_cycleLocation.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_cycleLocation.distanceFilter = 5;
_cycleLocation.delegate = self;
_startLocation = nil;
}
[_cycleLocation startUpdatingLocation];
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"%#",error);
if ( [error code] != kCLErrorLocationUnknown ){
[self stopLocationManager];
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
if (location.horizontalAccuracy < 0)
return;
[self.locations addObject:location];
NSUInteger count = [self.locations count];
if (count > 1) {
// only both to add new location if distance has changed by more than 5 meters
[self.locations addObject:location];
count = [self.locations count];
CLLocationCoordinate2D coordinates[count];
for (NSInteger i = 0; i < count; i++) {
coordinates[i] = [(CLLocation *)self.locations[i] coordinate];
}
MKPolyline *oldPolyline = self.polyline;
self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:count];
[self.mapView addOverlay:self.polyline];
if (oldPolyline)
[self.mapView removeOverlay:oldPolyline];
}
NSLog(#"didUpdateToLocation2: %#", location);
}
- (void) stopLocationManager {
[self.cycleLocation stopUpdatingLocation];
}
#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
NSLog(#"PolyLine Started");
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
[self.mapView setVisibleMapRect:overlay.boundingMapRect animated:YES];
renderer.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
renderer.lineWidth = 3;
return renderer;
NSLog(#"MKPolyline Rendering");
}
return nil;
}
- (IBAction)stopActivity:(id)sender {
[self stopLocationManager];
NSLog(#"Stopped");
}
-(void)distanceManager:(CLLocationManager *)manager didUpdateLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"Current Distance Started");
if (self.startLocation == nil)
{
totalDistanceBetween = 0;
_startLocation = newLocation;
}
CLLocationDistance distanceBetween = [newLocation distanceFromLocation:_startLocation ];
self.startLocation = newLocation;
totalDistanceBetween += distanceBetween;
NSString *cycleDistanceString = [[NSString alloc]
initWithFormat:#"%f",
totalDistanceBetween];
_CurrentDistance.text = cycleDistanceString;
NSLog(#"cycleDistance is %#", cycleDistanceString);
}
#end
.h :
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <AVFoundation/AVFoundation.h>
#interface StartCycleViewController : UIViewController <NSXMLParserDelegate, CLLocationManagerDelegate, MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
#property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
#property (nonatomic,strong) CLLocation *currentCycleLocation;
#property (nonatomic,strong) CLLocationManager *cycleLocation;
#property (nonatomic,strong) CLLocation *startLocation;
#property (strong, nonatomic) IBOutlet UILabel *CurrentDistance;
- (IBAction)stopActivity:(id)sender;
#property(nonatomic, strong) MKPolyline *polyline;
#property (nonatomic,strong) NSMutableArray *locations;
#property (nonatomic,strong) NSNumber *currentSpeed;
#property (nonatomic,strong) NSMutableArray *locationHistory;
- (void) stopLocationManager;
#end

Not getting the proper map using MKMapVIew

I have created a mapview using MKMapView. Everything is working fine but there is a difference in the map which i am getting and which i was supposed to get. In my view map is coming but not properly i guess. please see the difference and let me know what is the problem .
thanks
Here's my ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
mapView=[[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
mapView.delegate=self;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0},{0.0,0.0}};
region.center.latitude = 12.9667;
region.center.longitude = 77.5833;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[self.view addSubview:mapView];
DisplayMap *annotation = [[DisplayMap alloc]init];
annotation.title = #" Bangalore";
annotation.subtitle = #"Silver Jublee Park";
annotation.coordinate = region.center;
[mapView addAnnotation:annotation];
}
-(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 = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
return pinView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
#implementation DisplayMap
#synthesize coordinate,title,subtitle;
-(void)dealloc{
[title release];
[subtitle release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController<MKMapViewDelegate> {
MKMapView *mapView;
}
#end
#class DisplayMap;
#interface DisplayMap : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
I am getting the following map
I should get the following map
When I put the coordinates you're using into Google Maps I go to the same place that you are seeing, not the place you think you "should" be getting. Have you verified the coordinates? What are the coordinates of the lace you think you should be going to?
Here's a route between the two place http://goo.gl/maps/TrM3q
According to Goolge you are expecting iOS to should a place ~2000km away from the coordiantes you are using, which ain't gonna happen.

Annotations dont show title/subtitle on MapKit , iOS

I am parsing some data with some points of interests and trying to show them on map using the MapKit framework.
I created a custom class for my objects on the map which i call MyLocation.
MyLocation.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyLocation : NSObject <MKAnnotation> {
NSString *_name;
NSString *_address;
NSInteger _eventId;
CLLocationCoordinate2D _coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic , copy) NSString *name;
#property (nonatomic , copy) NSString *address;
#property (nonatomic, assign) NSInteger eventId;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
#end
MyLocation.m:
#import "MyLocation.h"
#implementation MyLocation
#synthesize name = _name;
#synthesize eventId=_eventId;
#synthesize address = _address;
#synthesize coordinate = _coordinate;
#synthesize title;
#synthesize subtitle;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name= [name copy];
_address = [address copy];
_coordinate = coordinate;
}
return self;
}
- (NSString *)title {
if ([_name isKindOfClass:[NSNull class]]) {
NSLog(#"11111111");
return #"Unknown charge";
}
else{
NSLog(#"222222");
return _name;
}
}
- (NSString *)subtitle {
NSLog(#"HAHAHAHAA");
return _address;
}
#end
Then on my viewController i do this :
//showing on map the points of interests that are stored on "eventList"
int i=0;
for (parsedItem *event in self.eventsList) {
CLLocationCoordinate2D coordinate;
coordinate.latitude = event.latitude;
coordinate.longitude = event.longitude;
if(i==0){
//numbers show map zoom. 500,500 = map stretches 500 meters to the North and the South of current location
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance (coordinate,1000,1000);
[mapView setRegion:region animated:NO];
}
MyLocation *eventAnnotation = [[MyLocation alloc] initWithName:event.typeEvent address:event.titleEvent coordinate:coordinate] ;
eventAnnotation.name = event.typeEvent;
eventAnnotation.address = event.titleEvent;
eventAnnotation.eventId = i;
i++;
[mapView addAnnotation:eventAnnotation];
}
Later i use some more code to give images to the annotations and everything appears on the map BUT if i click on the annotations i dont see the window with the title and the subtitle. I only see the annotations but without the extra information that i should get when i click on them.
I guess theres something wrong with this line of code :
MyLocation *eventAnnotation = [[MyLocation alloc] initWithName:event.typeEvent address:event.titleEvent coordinate:coordinate] ;
Any ideas?
EDIT
just to add that i also use this function:
- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
NSLog(#"I got into the class!!!");
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
MyLocation *eventAnnotation=annotation;
if ([eventAnnotation.address isEqualToString: #"Culture_07/02/2012"]) {
NSLog(#"Mphkeeee");
annotationView.image=[UIImage imageNamed:#"culture.png"];
//annotation.title = #"Holla!";
} else if ([eventAnnotation.address isEqualToString: #"Sports_06/29/2012"]) {
annotationView.image=[UIImage imageNamed:#"sports.png"];
} else if ([eventAnnotation.address isEqualToString: #"Accident_06/29/2012"]) {
annotationView.image=[UIImage imageNamed:#"accident.png"];
} else if ([eventAnnotation.address isEqualToString: #"Problem_06/29/2012"]) {
annotationView.image=[UIImage imageNamed:#"problem.png"];
} else if ([eventAnnotation.address isEqualToString: #"Traffic_07/02/2012"]) {
annotationView.image=[UIImage imageNamed:#"traffic.png"];
} else if ([eventAnnotation.address isEqualToString: #"Music_06/29/2012"]) {
annotationView.image=[UIImage imageNamed:#"music.png"];
}
return annotationView;
}
return nil;
}
Using a custom getter method for the title and subtitle instead of setting those properties explicitly is fine with the map view.
The reason your callouts are not showing is because title (which is returning _name -- which is set to event.typeEvent) is returning a nil or empty string.
Your getter method is checking if _name is of type NSNull but it could be an NSString and still be nil or an empty string.
When title is nil or an empty string, the annotation will not display the callout (even if canShowCallout is set to YES and even if subtitle is returning a non-empty value).
So it's most likely that typeEvent is nil or an empty string.
Looking at your code, you never actually set the title or subtitle attributes. In the place where you set up your title, do something like this:
eventAnnotation.title = event.typeEvent;
eventAnnotation.subtitle = event.titleEvent;
and make sure you have the title and subtitle properties set up in the .h file.
Edit: Here's the header for one of my annotations:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapmarkAnnotation : NSObject
<MKAnnotation>
{
NSString *title;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
#end
Please refer to the Apple doc. you need to set title and subtitle of an annotation object yourself. not some other property like _address instead. There is also 2 sample projects in that doc, check them out as well.

How to change title for annotation in ViewDidLoad?

I have code for mkannotation but I don't know how to change title in ViewDidLoad.
This is my code:
LocationView.h
...
#interface AddressAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
}
#end
#interface LocationView : UIViewController<MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
AddressAnnotation *addAnnotation;
}
#property (nonatomic, retain) Offers *offer;
#property (retain, nonatomic) IBOutlet MKMapView *mapView;
-(CLLocationCoordinate2D) addressLocation;
#end
LocationView.m
#import "LocationView.h"
#implementation AddressAnnotation
#synthesize coordinate;
- (NSString *)subtitle{
return #"Sub Title";
}
- (NSString *)title{
return #"Title";
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
//NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
#end
#interface LocationView ()
#end
#implementation LocationView
#synthesize mapView, offer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// I want in this method to change title
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location = [self addressLocation];
region.span=span;
region.center=location;
if(addAnnotation != nil) {
[mapView removeAnnotation:addAnnotation];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addAnnotation];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
//[mapView selectAnnotation:mLodgeAnnotation animated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
//:(MKMapView *)mapView
-(CLLocationCoordinate2D) addressLocation {
double latitude = 0.0;
double longitude = 0.0;
if(offer.lat > 0 && offer.lon > 0) {
latitude = [offer.lat doubleValue];
longitude = [offer.lon doubleValue];
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Why dont you simply synthesize the properties of your AddressAnnotation class and use something like
addAnnotation.mTitle = yourNewString;
Do I get your question wrong? This seems to be too obvious by far.
Edit
Of course, you have to give this back. For example like this:
- (NSString *)title{ return mTitle;}

Custom curent location Annotation pin not updating with core location

Trying to add a custom pin for current location, However the location does not update. Even after setting setShowsUserLocation = YES;
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if ([[annotation title] isEqualToString:#"Current Location"]) {
self.image = [UIImage imageNamed:[NSString stringWithFormat:#"cursor_%i.png", [[Session instance].current_option cursorValue]+1]];
}
However, if I set to return nil; everything works fine, but I lose the custom image. I really want to get this to work. Any help would be greatly appreciated.
As you've seen the setShowsUserLocation flag only shows the current location using the default blue bubble.
What you need to do here listen for location updates from the phone and manually reposition your annotation yourself. You can do this by creating a CLLocationManager instance and remove and replace your annotation whenever the Location Manager notifies its delegate of an update:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// update annotation position here
}
To reposition the coordinates, I have a class, Placemark, that conforms to the protocol MKAnnotation:
//--- .h ---
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface Placemark : NSObject <MKAnnotation> {
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, retain) NSString *strSubtitle;
#property (nonatomic, retain) NSString *strTitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;
#end
//--- .m ---
#implementation Placemark
#synthesize coordinate;
#synthesize strSubtitle;
#synthesize strTitle;
- (NSString *)subtitle{
return self.strSubtitle;
}
- (NSString *)title{
return self.strTitle;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c {
self.coordinate = c;
[super init];
return self;
}
#end
Then in my mapview controller I place the annotation with:
- (void) setPlacemarkWithTitle:(NSString *) title andSubtitle:(NSString *) subtitle forLocation: (CLLocationCoordinate2D) location {
//remove pins already there...
NSArray *pins = [mapView annotations];
for (int i = 0; i<[pins count]; i++) {
[mapView removeAnnotation:[pins objectAtIndex:i]];
}
Placemark *placemark=[[Placemark alloc] initWithCoordinate:location];
placemark.strTitle = title;
placemark.strSubtitle = subtitle;
[mapView addAnnotation:placemark];
[self setSpan]; //a custom method that ensures the map is centered on the annotation
}