Search code examples
iphoneobjective-csdkmapkitmkannotationview

Trying to Show User Location on Mapkit, the most annoying thing ever. Anyone lend a hand?


I'm following this tutorial (http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/) on adding mapkit and annotations to an application. However, i'm seriously struggling with the User Location. I'm new to xcode so not quite sure what to do next. I have tried Tony's option:

step one: add the CoreLocation framework to the project.

Step two: add this function to the iCodeMapViewController.m:


- (void)setCurrentLocation:(CLLocation *)location {
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
}

step three: add this code to the ViewForAnnotation Method:


if (annotation != mapView.userLocation) {
//the rest of the ViewForAnnotation code goes here
}else{
CLLocation *location = [[CLLocation alloc]
initWithLatitude:annotation.coordinate.latitude
longitude:annotation.coordinate.longitude];
[self setCurrentLocation:location];
} 

But when i go to build, it doesn't like it.

I've also tried this option:

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
 if ([annotation isKindOfClass:MKUserLocation.class]) return nil;
        //rest of code
}

The blue dot shows, my custom annotations show but the app crashes when i try and scroll through the table. The debugger gives no help but does stop on this statement.

Can someone please help? With code examples too? i think the answer to this post might be useful to a number of people also struggling with the mapkit.

Cheers


Solution

  • I had the same problem, but I managed to solve it. In cellForRowAtIndexPath I did this:

    
    NSMutableArray *annotations = [[NSMutableArray alloc] init];
        if(indexPath.section == 0)
        {
            for(MinuAsukohad *annotation in [mapView annotations])
            {
                if(![annotation isKindOfClass:[MKUserLocation class]])
                {  
                if([annotation annotationType] == MinuAsukohadTypeInterest)
                    {
                    [annotations addObject:annotation];
                    }
                }
            }
            cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
        }
    

    You just have to repeat it for all the sections.