Reputation: 1
-(MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
}
MKAnnotationView
method is used to display icon in map. How to call MKAnnotationView method in -didUpdateToLocation
method using objective-c?
Upvotes: 0
Views: 135
Reputation: 677
i'm not sure if this is what you want but :
here's the class that i've written for annotations :
.h file :
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyLocation : NSObject <MKAnnotation> {
NSString *_name;
NSString *_address;
CLLocationCoordinate2D _coordinate;
}
@property (copy) NSString *name;
@property (copy) NSString *address;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
@end
and the .m file :
#import "MyLocation.h"
@implementation MyLocation
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;
- (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]])
return @"Unknown charge";
else
return _name;
}
- (NSString *)subtitle {
return _address;
}
@end
and in the view controller i am calling location manager like :
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 10.0; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
and then in the location manager's delegate method i am setting my map like this:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
//CLLocationCoordinate2D newCoord = { newLocation.coordinate.latitude, newLocation.coordinate.longitude };
CLLocationCoordinate2D centerPoint = { newLocation.coordinate.latitude, newLocation.coordinate.longitude };
MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(0.01, 0.01);
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(centerPoint, coordinateSpan);
["your-map-name" setRegion:coordinateRegion];
["your-map-name" regionThatFits:coordinateRegion];
CLLocationCoordinate2D cordPoint = { latitude_value, longitude_value };
MyLocation *annotation = [[MyLocation alloc] initWithName:@"title of the pin" address:@"some sub string" coordinate:cordPoint] ;
["your-map-name" addAnnotation:annotation]; }
this is how i solved my annotation and map problem ..
i hope this helps..
Upvotes: 1