02 Aug 2010, 03:47
Ac_pragsmall

Anthony Cerra (2 posts)

I just bought and watched the MapKit tutorial, but I’m still having trouble getting the hang of MapKit. I’m trying to do something very simple. I want the app to center in on my current location and show a small region around my location. My code compiles fine, but does a force close when I try to run it.

What am I missing?

The logic behind my code is as follows: Create a region ojbect and 2D coordinate object. Set the coordinate object to be the user’s current location. Set the region center to be the coordinates of the user’s current location. Set an arbitrary span for testing purposes. Set the region to the view.

Thanks

`
- (void)viewDidLoad {
    [super viewDidLoad];
		
	MKCoordinateRegion region;
	CLLocationCoordinate2D coord = mapView.userLocation.coordinate;
	
	region.center.latitude = coord.latitude;
	region.center.longitude = coord.longitude;
	region.span.latitudeDelta = 0.5;
	region.span.longitudeDelta = 0.5;
	[self.mapView setRegion:region animated:YES];
	
}

`
22 Aug 2010, 02:45
Ac_pragsmall

Anthony Cerra (2 posts)

After watching the MapKit video a few more times and reading these forums extensively, I was able to solve my problem. For anyone following along here is the code:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

{
	MKCoordinateRegion region;

	region.center.latitude = self.mapView.userLocation.coordinate.latitude;
	region.center.longitude = self.mapView.userLocation.coordinate.longitude;
	
	region.span.latitudeDelta = 0.1;
	region.span.longitudeDelta = 0.1;
	
	[self.mapView setRegion:region animated:YES];
	
	return nil;
}

</code>

  You must be logged in to comment