How to get the user's current location by code in iphone app?

前端 未结 4 1153
北荒
北荒 2021-01-07 10:17

I want to get the user\'s current location from my iPhone app. I want to show the user\'s current location like country name, latitude, longitude information in my app. And

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 11:00

    First create an instance of the MKMapView

    To get user's latitude and longitude:

    In your viewDidLoad

    [yourMapView setShowsUserLocation:YES];
    
    CLLocationCoordinate2D userCoord;
    
    userCoord.latitude=map_view.userLocation.coordinate.latitude;
    userCoord.longitude=map_view.userLocation.coordinate.longitude;
    
    //NSLogging these on simulator will give you Cupertino or whatever location you set in location simulation.
    

    And for country name you will need reversegeocoding you can look at the class reference here

    https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40008323

    OR

    If MKReverseGeoCoding gets too complicated you can use Yahoo's reversegeocoder

    http://where.yahooapis.com/geocode?q=%f,%f&gflags=R&appid=yourAppId, those 2 %f will be userCoord.longitude and userCoord.latitude.

提交回复
热议问题