How to get Location (latitude & longitude value) in variable on iOS?

后端 未结 3 1856
南旧
南旧 2020-12-15 07:41

I would like to use the location of my app user, more specifically the latitude and longitude value.

I need them to use in a variable, so that I can send them with

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 08:19

    You can use CoreLocation to get the longitude and latitude.

    Include framework:

    Click your project in navigator.

    Click the plus button under "link binary with libraries"

    Add Corelocation to your project.

    Import the header file:

    #import 
    

    Declare CLLocationManager:

    CLLocationManager *locationManager;
    

    initialize locationManager:

    - (void)viewDidLoad
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        [locationManager startUpdatingLocation];
    }
    

    Then, use

    float latitude = locationManager.location.coordinate.latitude;
    float longitude = locationManager.location.coordinate.longitude;
    

提交回复
热议问题