Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//YourViewController.h
@interface YourViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>{
    IBOutlet MKMapView *map;
    BOOL isLocating;

}
@end

//YourViewController.m
#import "YourViewController.h"
#import "MapPlaceMark.h"

@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];

}


#pragma mark LocationMethods
- (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
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.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
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.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