Following Online tutorials I made a MKMapView working, but I have a couple of problems:
First problem: Map show my current position, but if I try to scroll that map It will automatically return to my position, so, for example, I am not able to see what streets are nearby, because map will center back to my current position.
Second problem: I'd like to implement some "pinch to zoom" as if you tap on my map it will get that tap point's longitude and latitude.
I surely messed up my code, because I mixed two tutorial in one ... trying to archive what I needed from my "app". :-)
This is in my .h:
@interface mappa2 : UIViewController <CLLocationManagerDelegate> {
MKMapView *mapview;
CLLocationManager *manager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapview;
And this is my messy .m
@synthesize mapview;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Procedura per TAP:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.mapview addGestureRecognizer:tapRecognizer];
[self startMap]; }
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"Errore %@",error);
NSLog(@"Non ho potuto calcolare la posizione");
}
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"Location: %@",newLocation);
CLLocation *currentLocation = newLocation;
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] >0 ) {
placemark = [placemarks lastObject];
}
else NSLog(@"%@",error.debugDescription);
}];
MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0} };
region.center.latitude = currentLocation.coordinate.latitude;
region.center.longitude = currentLocation.coordinate.longitude;
region.span.longitudeDelta = 0.001f;
region.span.latitudeDelta = 0.001f;
[mapview setRegion: region animated:YES];
[mapview setScrollEnabled:YES];
}
- (void) startMap {
manager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation];
mapview.mapType = MKMapTypeStandard;
mapview.showsUserLocation = YES;
}
So .. I'd like that my map could point to my current position when "view did load", but then I'd be able to move around that point! ... and zooming in and out with gesture... is that too much for a newbie?
Thank in advance!