[英]How to find current user location to use in Google Places API for iOS
[英]User Current Location Not Find
在我的一個項目中,我必須將用戶的當前位置發送到Appdelegate中的Web服務器。 當用戶單擊顯示的警報的``確定''按鈕以獲取用戶許可時,它運行良好,但是在我第一次運行該應用程序時,進入rootview Controller后,許可警報顯示得較晚。得到的歸類為0.00000f。如何在appdelegate中強制獲得許可或強制獲得用戶位置。
再考慮一下,如果用戶拒絕定位其位置。警報沒有顯示其他時間。在這種情況下,也得到了限制的緯度和經度是0.00000f。我如何在同一應用程序中顯示2次或兩次以上的權限警報。希望你能理解我的問題。
快樂編碼
根據您的rootViewController
,您必須先設置CLLocationManager
然后再設置Window中的rootViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
對於第二件事 ,默認情況下只詢問一次警報,如果用戶授予權限,則調用CLLocationManagerDelegate method
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
並獲取當前位置的緯度和經度。如果用戶拒絕了位置服務,則必須參考此信息 。
您需要在AppDelegate
的界面中設置CLLocationManagerDelegate
。
為了啟動CLLocationManager獲取位置,必須設置CLLocationManager類的某些屬性。 為此,實現以下代碼片段:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Location manager settings
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
// Your code for rootViewController settings here
}
然后,按如下方式實現CLLocationManagerDelegate
方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *userLocation = [locations lastObject];
}
userLocation
對象將是用戶的當前位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.