Jon
Jon

Reputation: 195

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

Upvotes: 0

Views: 1521

Answers (2)

Shiim
Shiim

Reputation: 26

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.

Upvotes: 1

rob
rob

Reputation: 45

Sounds like you are trying to include your current location as one of the cells in the table ... look at your console and give us the output when the crash happens.

Upvotes: 0

Related Questions