【问题标题】:How to get users current location on app launch如何在应用启动时获取用户当前位置
【发布时间】:2013-12-19 06:17:31
【问题描述】:

我只想在应用程序启动时获取用户当前位置(纬度,经度)..我不想使用委托方法进行频繁的位置更新..我不使用地图只需要启动时的当前位置..应该如何我这样做??

我正在使用下面的代码,但它不起作用。

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

CLLocation *location;
location = [locationManager location];
CLLocationCoordinate2D coord;
coord.longitude = location.coordinate.longitude;
coord.latitude = location.coordinate.latitude;
NSLog(@"latitude %f longitude %f",coord.latitude,coord.longitude);

输出:- 纬度 0.000000 经度 0.000000

请帮忙..

【问题讨论】:

  • 嘿,你必须通过委托方法来获取用户的当前位置
  • 获取位置后,您必须使用委托方法,您可以停止位置管理器的位置更新
  • [locationManager stopUpdatingLocation];
  • 谢谢大家......使用委托方法获取位置,然后使用 [locationManager stopUpdatingLocation] 停止未来更新。

标签: ios mapkit core-location


【解决方案1】:

最好使用委托方法来获取用户位置,并在 didupdateLocation 方法中停止它 我认为这个网址可以帮助你 iPhone SDK: Track users location using GPS 并在

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D here =  newLocation.coordinate;
NSLog(@"%f  %f ", here.latitude, here.longitude);
[locationManager stopUpdatingLocation];
}

你已经完成了......

【讨论】:

    【解决方案2】:
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        // NSLog(@"didUpdateToLocation: %@", newLocation);
        CLLocation *currentLocation = newLocation;
    
        // Reverse Geocoding
        //  NSLog(@"Resolving the Address");
        [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            //  NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
            if (error == nil && [placemarks count] > 0) {
                placemark = [placemarks lastObject];
                [locationManager stopUpdatingLocation];
    
            } else {
                NSLog(@"%@", error.debugDescription);
            }
        } ];
    }
    

    【讨论】:

    • 是否可以不使用此委托方法获取用户位置??
    • 不,它不能,根据这种方式,委托方法将调用一次,因此它不会给应用程序带来额外的负载
    【解决方案3】:

    使用CLLocationManagerDelegate方法

    - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation
    {
        [_locationManager stopUpdatingLocation];
        NSLog(@"latitude %f longitude %f",newLocation.coordinate.latitude,,newLocation.coordinate.longitude);
    
    }
    

    iOS6 已弃用上述方法,请改用locationManager:didUpdateLocations:

    - (void)locationManager:(CLLocationManager *)manager
         didUpdateLocations:(NSArray *)locations
    

    【讨论】:

      【解决方案4】:
      import <CoreLocation/CoreLocation.h>
      
      delegate: CLLocationManagerDelegate
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          // System alert for Allowing current location of user.
      
          // Location manager object creation.
          locationManager = [[CLLocationManager alloc] init];`enter code here`
      
          locationManager.delegate = self;
          locationManager.distanceFilter = kCLDistanceFilterNone;
          locationManager.desiredAccuracy = kCLLocationAccuracyBest;
          [locationManager startUpdatingLocation];
      }
      
      
      // iOS >= 6.0.
      - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
      {
          [locationManager stopUpdatingLocation];
      }
      
      // iOS < 6.0
      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
      {
          [locationManager stopUpdatingLocation];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-20
        • 2023-03-03
        相关资源
        最近更新 更多