0

I have tried using this method, but for some reason it wont zoom in on the current location. Is there something I am doing wrong?

Appdelegate.m

 -(void)handleNetworkChange
{
    self.reachability = [Reachability reachabilityForInternetConnection];
    [self.reachability startNotifier];
    NetworkStatus remoteHostStatus = [self.reachability currentReachabilityStatus];
    MapViewController *mapViewController = [[MapViewController alloc]init];
    if(remoteHostStatus == NotReachable)
    {
        self.internetReachable = NO;
    }
    else
    {
        self.internetReachable = YES;
        [mapViewController userLocation];
    }    
}

MapViewController.m

-(void)userLocation
{
    MKCoordinateRegion mapRegion;
    mapRegion.center.latitude = self.mapView.userLocation.coordinate.latitude;
    mapRegion.center.longitude = self.mapView.userLocation.coordinate.longitude;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;
    [self.mapView setRegion:mapRegion animated:YES];
}
iOS Dev
  • 4,143
  • 5
  • 30
  • 58
user3249524
  • 71
  • 2
  • 9
  • is your method userLocation getting called from app delegate – Gajendra Rawat Mar 10 '14 at 15:50
  • Yes, I can confirm it is being called. – user3249524 Mar 10 '14 at 15:54
  • where do you show (present) the `mapViewController`, and why you create an instance of `MapViewController` every time `handleNetworkChange` method is called? – iOS Dev Mar 10 '14 at 15:55
  • I only show instances that way as an example, but the way I show mapviewcontroller is through navigation controller not through appdelegate. – user3249524 Mar 10 '14 at 15:59
  • First you can log the mapView userLocation coordinates, to see if there are the right ones, then try to change deltas from 0.2 to 0.005. – iOS Dev Mar 10 '14 at 16:06
  • Regardless of the "Reachability logic", is showsUserLocation set to YES? But most important: `mapView.userLocation` should be accessed in the didUpdateUserLocation delegate method so you can be reasonably sure the map view actually has a location. Outside of that delegate method, you must first check if `mapView.userLocation.location` is `nil`. Additionally, if you use the userTrackingMode, you shouldn't have to worry about this in first place. –  Mar 10 '14 at 17:01
  • You don't seem to be marking any answers as `top answer`, upvoting or leaving comments on any of them. If an answer solved your issue, you need to mark it as top answer / upvote it so others facing the same issue will know what solved it. The people who receive these will get rep points in exchange for putting in some time to help you. Please go to your profile and go to each of your questions. If you have an answer that solved it, mark it as top and upvote it, also upvote any that offered something helpful to you – Simon McLoughlin Mar 26 '14 at 09:42
  • Also note other users will be very unlikely to answer your questions if you don't use the site correctly and return the favour to those that are helping you out – Simon McLoughlin Mar 26 '14 at 09:43

1 Answers1

0

I would simply use NSNotificationCenter. you set a listener that waits for a post notification, once the post norification is received it will fire a selector

here is how to do it (very simple actually)

This needs to go in your view controller where you present the map, best in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(runThisFunctionWhenReady:) 
        name:@"TestNotification"
        object:nil];

he zoom method should be placed in the same viewcontroller - like so:

-(void)runThisFunctionWhenReady
{
   // Zoom the map and other funky stuff
}

Now, from anywhere in the app (delegate included) you can fire this by posting a notification like this:

 [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];
chewy
  • 8,207
  • 6
  • 42
  • 70