Getting center of visible region - MapKit

Discussion in 'iOS Programming' started by Jeremy1026, Jun 25, 2009.

  1. Jeremy1026 macrumors 68020

    Jeremy1026

    Joined:
    Nov 3, 2007
    #1
    I am trying to find the center of visible region using MapKit.

    Code:
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    	location=newLocation.coordinate;
    	//One location is obtained.. just zoom to that location
    	
    	NSLog(@"%@",oldLocation);
    	
    	if (!oldLocation) {
    		
    		MKCoordinateRegion region;
    		region.center=location;
    		//Set Zoom level using Span
    		MKCoordinateSpan span;
    		span.latitudeDelta=1.5;
    		span.longitudeDelta=1.5;
    		region.span=span;
    		
    		NSLog(@"center = %f",newLocation.coordinate);
    		
    		[mapView setRegion:region animated:TRUE];
    		
    	}
    	
    }
    
    That returns the proper center. Unforunately, I can't figure out what to do in - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    I tried doing
    Code:
    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    	NSLog(@"Scrolling");
    	MKCoordinateRegion region;
    	region.center = location;
    	NSLog(@"center = %f, %f",location.latitude, location.longitude);
    
    }
    
    This only shows the current location of the device, not the center of the map. Any help would be appreciated.
     
  2. Jeremy1026 thread starter macrumors 68020

    Jeremy1026

    Joined:
    Nov 3, 2007
    #2
    Mission sucessful!

    Code:
    
    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
         MKCoordinateRegion region;
         centerCoordinate=mapView.region.center;
         region.center= centerCoordinate;
    	
         NSLog(@"%f,%f",centerCoordinate.latitude, centerCoordinate.longitude);
    }
    	
    
     
  3. idheeraj, Nov 23, 2012
    Last edited by a moderator: Nov 23, 2012

    idheeraj macrumors newbie

    Joined:
    Nov 23, 2012
    #3
    Mission Completed !

    Code:
    - (void)mapView:(MKMapView *)mapView1 regionDidChangeAnimated:(BOOL)animated {
        MKCoordinateRegion region;
        [B]CLLocationCoordinate2D[/B] centerCoordinate;
        
        centerCoordinate=mapView1.region.center;
        region.center= centerCoordinate;
    	
        NSLog(@" %f,%f",centerCoordinate.latitude, centerCoordinate.longitude);
    }
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    This is a three-and-a-half-year-old post, and you posted the same code that the OP posted himself. Why resurrect such an old thread, especially when you don't have anything meaningful to add?
     

Share This Page