开发者

How to load the map with the obtained current location in iphone?

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];
}开发者_如何学C

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


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


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;
    }
}


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]];
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜