tags:

views:

287

answers:

3

I am new to iphone development.I am creating a map application.I have toolbar below the mapview with a button on it.On clicking the button it displays as an alert to load the current location.In my button click even i hava given code to find current location

 -(IBAction) gosearch : (id) sender{
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; 
[locationManager startUpdatingLocation];
}

For me it is not displaying the alert.What should i do?Please help me out. Thanks.

A: 

Make sure for the alert view it looks like this-

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Current Location" message:@"Show Current Location?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK"];
        [alert show];

and also

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        map.showsuserlocation = YES;
    }
}
Flafla2
by default it will not give an alert for my above code?
Warrior
Well, I posted this code just to play it safe.
Flafla2
+1  A: 

You need to have your controller class implement protocol CLLocationManagerDeligate. This will mean that it gets notified of errors or when the location is posted (for example – locationManager:didUpdateToLocation:fromLocation:)

Then you can pass on the long/lat and radius of view required to the MapView

buggles
A: 

I'm not sure I fully understand your questions but here's what I have done to use the location manager.

First, I declare a class that implements CLLocationManagerDelegate

@interface GPSComponent <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
}

Then, in the class I have:

- (id) init {
    locationManager = [[CLLocationManager alloc] init];

    // Provide the best possible accuracy (this is the default; just want to write some code).
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    // Must move at least 100 meters to get a new location update (default is get all notifications).
    locationManager.distanceFilter = 100;   

    locationManager.delegate = self;

    [locationManager startUpdatingLocation];
}

#pragma mark -
#pragma mark CLLocationManagerDelegate methods

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // If you are about 400 miles south off the coast of Ghana, we might be ignoring your location information. We apologize.
    // This is a lazy way to check for failure (in which case the struct is zeroed out).
    if((fabs(newLocation.coordinate.latitude) > 0.001) || (fabs(newLocation.coordinate.longitude) > 0.001)) {
        NSLog(@"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
        if (currentLocation != nil) {
            [currentLocation release];
        }
        currentLocation = newLocation;
        [currentLocation retain];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Location Manager error");
}

Then, to display a map with the user's location:

// Pull out the longitude and latitude and invoke google maps
- (IBAction)mapItButtonPressed {
    NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%f,%f", (float)currentLocation.coordinate.latitude, (float)currentLocation.coordinate.longitude]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} 
Randy Simon
What is the function of your code
Warrior
As I said, I wasn't terrible clear about your question. I thought that you were having problem the location manager so this code shows an example of how to use it.
Randy Simon