如何使用获得的当前位置在iphone中加载地图?

时间:2010-02-17 15:05:38

标签: iphone maps

我是iphone开发的新手。我正在创建一个地图应用程序。我在mapview下面有一个工具栏,上面有一个按钮。单击按钮,它会显示为加载当前位置的警报。在我的按钮中单击甚至我hava给出代码来查找当前位置

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

对我而言,它没有显示警报。我该怎么办?请帮帮我。感谢。

3 个答案:

答案 0 :(得分:1)

您需要拥有控制器类实现协议CLLocationManagerDeligate。这意味着它会收到错误通知或位置发布时的通知(例如 - locationManager:didUpdateToLocation:fromLocation :)

然后你可以传递MapView

所需的长/纬度和视角

答案 1 :(得分:0)

确保警报视图看起来像这样 -

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

以及

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        map.showsuserlocation = YES;
    }
}

答案 2 :(得分:0)

我不确定我是否完全理解您的问题,但这是我使用位置管理员所做的工作。

首先,我声明一个实现CLLocationManagerDelegate

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

然后,在课堂上我有:

- (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");
}

然后,显示包含用户位置的地图:

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