//YourViewController.h
@interface YourViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>{
IBOutlet MKMapView *map;
BOOL isLocating;
}
@end
//YourViewController.m
@implementation YourViewController {
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
- (void) viewDidLoad {
[NSTimer scheduledTimerWithTimeInterval:60000 target:self selector:@selector(updateLocation) userInfo:nil repeats:YES];
}
- (void) updateLocation {
isLocating = YES;
CLLocationManager *locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
if (![locationManager locationServicesEnabled])
{
//replace with alert
[atextfield setText:@"Location Services Are Not Enabled"];
}
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
if(isLocating == YES){
CLLocationCoordinate2D location = [newLocation coordinate];
//One location is obtained.. just zoom to that location
NSLog([NSString stringWithFormat:@"%f,%f", location.latitude, location.longitude]);
MKCoordinateRegion region;
region.center=location;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta=.090;
span.longitudeDelta=.090;
region.span=span;
[map setRegion:region animated:TRUE];
MapPlaceMark *placemark=[[MapPlaceMark alloc] initWithCoordinate:location];
[map addAnnotation:placemark];
[placemark release];
isLocating = NO;
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[atextfield setText:@"Location search failed"];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *identifier = @"com.companyname.pin";
MKAnnotationView *pin = [ mapView dequeueReusableAnnotationViewWithIdentifier:identifier ];
if ( !pin )
{
if ( annotation == [map userLocation]) {
pin = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:identifier ];
}
else{
pin = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:identifier ];
}
}
pin.annotation = annotation;
return pin;
}
}
@end
//MapPlaceMark.h
@interface MapPlaceMark : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *subTitle;
NSString *Title;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (readwrite, retain) NSString *subTitle;
@property (readwrite, retain) NSString *Title;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;
@end
//MapPlaceMark.m
@interface MapPlaceMark : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *subTitle;
NSString *Title;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (readwrite, retain) NSString *subTitle;
@property (readwrite, retain) NSString *Title;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;
@end