iPhone current user location coordinates showing as (0,0)

后端 未结 8 921
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 12:28

I\'m trying to get the users current latitude and longitude with this viewDidLoad method. The resulting map is correctly indicating the current location however the NSLog c

相关标签:
8条回答
  • 2020-12-03 12:51

    The map will not try to get the users location until it's actually displayed, as far as I know. A good test of that is, when do you get the dialog asking you if it's OK for the app to get the users location? In my experience that has always come up only after the map shows on screen.

    The best way to approach this problem if you must have a location on startup, is to implement the classic Core Location handler code and get the location from that initially. You can stop receiving updates from there once the map is up and get further changes from there (though if you need constant updates you are better off just keeping with the standard Core Location updates. If you think about it, when you have "shows user location" enabled in the map further use of Core Location by your app is essentially free since the GPS will be fired up the whole time anyway.

    0 讨论(0)
  • 2020-12-03 12:53

    As Kendall said, the MKMapView does not have a user location until it finishes loading, and the viewDidLoad method is usually too early to request it. A simpler way than using CoreLocation is to implement the MKMapViewDelegate method -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation. It will give you the user's location as soon as it is located, without having to deal with a CLLocationManager.

    0 讨论(0)
  • 2020-12-03 12:56

    If you try to get user co-ordinates in viewDidLoad() method, it will always give 0,0 result, because it is not yet initialized.

    You should take a look at "Where Am I" sample code form Apple site. It explains what you need. You need to use CLLocationManager class. You will also need the method

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

    This method will be called automatically every time when user location is updated. If you are using simulator, then this method will be called only once and it will return some default co-ordinates.

    Hope it helps.

    0 讨论(0)
  • 2020-12-03 13:04

    This answer explains how to set a map view with a user location:

    HowTo initialise MKMapView with a given user location?

    0 讨论(0)
  • 2020-12-03 13:07

    Add this to your viewDidLoad section

    mapView.showsUserLocation = YES;
    
    0 讨论(0)
  • 2020-12-03 13:08

    You should get the coordinate from CoreLocation instead of MapKit. MapView will display the user's location after it actually got current location. Before the location is determined, the latitude and longitude are both set to 0. 3

    You should create a CLLocationManager instace, set delegate. You will be notified when the location is determined.

    0 讨论(0)
提交回复
热议问题