繁体   English   中英

显示我的iPhone中的当前位置

[英]Showing current location in my iphone

我正在实施从当前位置到目的地位置的路线方向。

当我进入特定页面时,我的应用程序崩溃了。 但如果我再次运行该应用程序,它会显示路线方向。

我发现问题是第一次,当前位置显示为0.00000,0.00000坐标。 我第二次启动应用时,当前位置正确显示。

出了什么问题? 我想在第一时间正确地获得我当前的位置。

我正在编写下面的代码。 请检查一下,让我知道我的代码中的问题在哪里

#import "MapWithRoutesViewController.h"
#import "TFSViewController.h"

@implementation MapWithRoutesViewController

@synthesize locationManager,lat,lon;

- (void) updateCurrentLabel {
    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];

    locationlable.text = [NSString stringWithFormat: @"Current Location: %@,%@", latitude, longitude];
}

- (void)viewDidLoad {
    [self getCurrentLocation];
    [super viewDidLoad]; 
}

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self updateCurrentLabel]; 
}

-(void) getCurrentLocation {
    MapView* mapView = [[[MapView alloc] initWithFrame:
                         CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height)] autorelease];      [self.view
addSubview:mapView];
    Place* home = [[[Place alloc] init] autorelease];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;

    if (locationManager==NO) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for
this app."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    } else {
        //        if (locationManager==NO)
        //            [mapView.lo addObserver:self forKeyPath:@"location"
        // options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
        // context:nil];
    }

    locationManager.pausesLocationUpdatesAutomatically = NO;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    [locationManager setPausesLocationUpdatesAutomatically:YES];
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];


    CLLocation *Loc = [locationManager location];
    CLLocationCoordinate2D coordinate = [Loc coordinate];

    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];


    NSLog(@"lat,lon %@%@",latitude,longitude);

    home.name = @"";
home.description = @"";
home.latitude = coordinate.latitude;
home.longitude = coordinate.longitude; 

    Place* office = [[[Place alloc] init] autorelease];
office.name = @"";
office.description = @"";
office.latitude = 22.0000;
    office.longitude = 88.0008;

    [mapView showRouteFrom:home to:office];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. 
}

-(IBAction)back:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)tfs:(id)sender{
    TFSViewController *tfs = [[TFSViewController alloc]initWithNibName:@"TFSViewController" bundle:nil];

    [self presentViewController:tfs animated:YES completion:nil];
}

@end

麻烦的是你正在制作位置管理员,然后期望它在几微秒后就有一个位置。 您应该设置位置管理器的委托,然后等待它被调用。 当LM有一个位置时,它将调用didUpdateLocation,你可以用它来调用updateCurrentLabel

您应该使用委托方法-locationManager:didUpdateLocations:来获取位置。 CLLocationManager需要几秒钟来建立GPS连接并确定当前位置。

http://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didUpdateLocations

您还可以使用CLLocation的horizo​​ntalAccuracy属性来检查位置的准确性,以确定是要使用该值还是丢弃该值。

http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/occ/cl/CLLocation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM