tags:

views:

24

answers:

1

Hi Guys,

I am creating annotations from database. But when I click on annotation to show bubble application crashes. But when I hardcode the coordinates and create placemarks on map the bubbles popup successfully. below is the code I am fetching from database and loading annotations.

-(void) mapView:(MKMapView *)imnMapView didAddAnnotationViews:(NSArray *)views; {

    BOOL *regionSet=FALSE;


                while(sqlite3_step(compiledStatement) == SQLITE_ROW)
                {
                    CLLocationCoordinate2D userCoods1;
                    NSString *lat = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 3)];
                    NSString *longg = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 4)];
                    float fLat = [lat floatValue];
                    float fLong = [longg floatValue];


    userCoods1.latitude = (CLLocationDegrees)fLat;

    userCoods1.longitude = (CLLocationDegrees)fLong;

                    if(regionSet == FALSE)
                    {
                        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userCoods1,400000,500000);
                        [mapView setRegion:region animated:YES];
                        regionSet=TRUE;
                    }

                    NSString *siteName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];
                    NSString *siteAddress = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 2)];


                    ParkPlaceMark *placemark=[[ParkPlaceMark alloc] initWithCoordinate1:userCoods1 title:siteName subtitle:siteAddress atm:@"ATM"];
                    pDescription = @"ATM";

                    [mapView addAnnotation:placemark];
                    [placemark release];

                }

            }               

            sqlite3_finalize(compiledStatement);

        }   

}

And When i just change the userCoords hardcoded and create three different placemarks the bubbles popup and works fine. Please help me getting out of this. thanks in advance.

A: 

Why don't you save the latitude and longitude values as a decimal or as a float in the database? It is way more easier to convert it.

Can you log the values, using NSLog, that are in lat and longg? Are they converted and stored right in flat and flong?

NSLog(@"String - Latitude: %@ Longitude: %@", lat, longg);
NSLog(@"Float  - Latitude: %f Longitude: %f", fLat, fLong);
Jeroen de Leeuw
Its not problem with Lat long. The annotations appear at the right place. The only problem is when i click on annotation, the application crashes with Exec_Bad_Access.
Amarpreet
Are you sure that the title and subtitle for the annotation are correctly set? Does the bubbles popup when you touch them or does the application crash before that?
Jeroen de Leeuw
Yes I am sure the title and subtitle set correctly. And when I click on bubble, app crashes. App crashes before bubble popup
Amarpreet
Maybe you must add the annotations before your calling the function didAddAnnotationViews: , what's in your viewForAnnotation: method?
Jeroen de Leeuw
But its working fine when i add placemarks with hardcoded values. Which menas its at the right place.
Amarpreet
It's not how it's intended by Apple, look at the documentation http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html By the time this method is called, the specified views are already added to the map. This means that the annotations, or the annotationviews, must already be added to the map when you call this method.
Jeroen de Leeuw
What's in your viewForAnnotation: method?
Jeroen de Leeuw
MKPinAnnotationView *view =[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"]; if(!view) { view=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOTATION"]; if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; } [view setCanShowCallout:YES]; return [view autorelease];
Amarpreet
Did you find anything? This is pretty starightforward code. I dont know where i missed something. Please help.
Amarpreet
Yes you were right , the cause of error were title and subTitle strings. I am stuck please help what to do. When I tried with hard coded values for title and subtitle it worked. please tell me what to do.
Amarpreet
Is it already working now? Are you sure that the values you retrieve from the database are correct NSString values?
Jeroen de Leeuw
Yes those were string values but i found another way of doing this. Thanks for your effort friend. :)
Amarpreet