如何在iOS上获取当前位置? [英] How Can I Get Current Location on iOS?

查看:101
本文介绍了如何在iOS上获取当前位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得当前位置。当我在不同的地方启动我的应用程序时,App可以获得最后的位置。但我不想最后的位置。如果您关闭应用并重新启动它,现在应用可以获取当前位置。即使首次启动应用程序,如何获取当前位置?

I can't get current location. When I launch my app in different place, App can get last location. But I don't want to last location. If you close the app and restart it, now app can get current location. How can I get current location even if it is first launch of application?

- (void)viewDidLoad
{
    [super viewDidLoad];

    [locationManager startUpdatingLocation];

    self.mapView.delegate = self;
    [self.mapView setShowsUserLocation:YES];

    locationManager.delegate=self;

    locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;

    location = [locationManager location];

    CLLocationCoordinate2D coord;
    coord.longitude = location.coordinate.longitude;
    coord.latitude = location.coordinate.latitude;
    lat = coord.latitude;
    longt = coord.longitude;
}


推荐答案

你在做 [locationManager startUpdatingLocation]; 在设置其委托之前

You are doing [locationManager startUpdatingLocation]; before setting its delegate

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;

[locationManager startUpdatingLocation];

并实施其委托方法

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{

}

这篇关于如何在iOS上获取当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆