views:

34

answers:

0

Hi all, i have a class for my geocoding (locationManager and reverseGeocoder). After the first load i see my location in my lable without any problems. after leave the view and load the view again i see in my lable http response-codes ("User_Agent" Accepted....) or get a bad_access and my app freeze.

here is my code for this class:

#import "GeoController.h"
#import "MapViewAnnotation.h"


@implementation GeoController
@synthesize coordinate;


NSString *street;
NSString *housenumber;
NSString *currentLocation;



- (void)viewDidLoad {

    [super viewDidLoad];




    [[mapView layer] setCornerRadius:10];
    [mapView setClipsToBounds:YES];

    [[self view] addSubview:mapView];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter=kCLLocationAccuracyNearestTenMeters;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];


    [self startUpdatingLocation];

}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {
    [super viewDidUnload];
}



#pragma mark CLLocationManager
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation 

    fromLocation:(CLLocation *)oldLocation{

    float avgAccuracy = (newLocation.verticalAccuracy + newLocation.horizontalAccuracy)/2.0;

    if (avgAccuracy <= 300){
        [locationManager stopUpdatingLocation];
        locationManager.delegate = nil;


        reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];

        reverseGeocoder.delegate = self;
        [reverseGeocoder start];
    }

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [self showErrorView:error setErrorTitle:@"GPS Error"];

}

#pragma mark reverseGeocoder
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{




    if (placemark.thoroughfare == nil)
    {
        street = @"";
    }
    else 
    {
        street = placemark.thoroughfare;
    }


    if (placemark.subThoroughfare == nil)
    {
        housenumber = @"";
    }
    else 
    {
        housenumber = placemark.subThoroughfare;
    }



    currentLocation = [NSString stringWithFormat:@"%@%@%@%@%@%@%@", street,
        @" "housenumber,@", \n",placemark.postalCode, @" ",placemark.locality;

    locationName.text = currentLocation;
    NSLog(currentLocation);


    [self showMap:placemark];

}


- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
    [self showErrorView:error setErrorTitle:@"Error"];
}

#pragma mark ErrorView

-(void)showErrorView:(NSError *) error setErrorTitle:(NSString *) title{
    NSDictionary *errorDictionary = error.userInfo;
    NSString *errorMessage = [NSString stringWithFormat:@"%@%@%@", @"Errormessage\n",
                              @" \n",
                              [errorDictionary objectForKey:@"NSLocalizedDescription"]];

    UIAlertView *errorView = [[[UIAlertView alloc] initWithTitle:title
                                                         message:errorMessage
                                                        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];

    [errorView show];

}


-(void)showMap:(MKPlacemark *) placemark
{

    MKCoordinateRegion region;

    region.center = placemark.coordinate;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;

    MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithCoordinate:placemark.coordinate];

    mapAnnotation.title = [NSString stringWithFormat:@"%@%@%@", 
                           placemark.thoroughfare, @" ",
                           placemark.subThoroughfare];

    mapAnnotation.subtitle = [NSString stringWithFormat:@"%@%@%@", 
                              placemark.postalCode, @" ",
                              placemark.locality];


    [mapView setRegion:region animated:NO];
    [mapView addAnnotation:mapAnnotation];
    [mapAnnotation release];

}



- (void)dealloc {
    [super dealloc];
    //[locationManager release];
    //[reverseGeocoder release];
    [housenumber release];
    [street release];
}


@end

thx for your help :)

Andi