获取用户位置不起作用?
Getting user location not working?
我是 MapView 新手 topic.Now 我正在处理地图 view.I 正在获取旧金山位置的经度和纬度 values.I 正在 simulator.It 进行测试未显示当前位置的经度和纬度值。
借助本教程http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/
我正在开发应用程序。
在 AppDelegate 文件中我写了如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView * alert;
//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
} else{
self.locationTracker = [[LocationTracker alloc]init];
[self.locationTracker startLocationTracking];
//Send the best location to server every 60 seconds
//You may adjust the time interval depends on the need of your app.
NSTimeInterval time = 60.0;
self.locationUpdateTimer =
[NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:@selector(updateLocation)
userInfo:nil
repeats:YES];
}
return YES;
}
我在 ViewController
中导入了位置追踪器 class
我写了下面的代码来获取位置名称和地址
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
[geoCoder reverseGeocodeLocation:self.appDelgate.locationTracker.myLastLocation_ completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:@"%@ %@",placemark.subLocality,placemark.subAdministrativeArea];
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:@"address"];
}
}];
现在我的问题是它没有进入 block.So 我得到 "returnAddress" 作为(空)。
虽然没来,我也是这么写的
- (void)updateLocationToServer {
NSLog(@"updateLocationToServer");
// Find the best location from the array based on accuracy
NSMutableDictionary * myBestLocation = [[NSMutableDictionary alloc]init];
for(int i=0;i<self.shareModel.myLocationArray.count;i++){
NSMutableDictionary * currentLocation = [self.shareModel.myLocationArray objectAtIndex:i];
if(i==0)
myBestLocation = currentLocation;
else{
if([[currentLocation objectForKey:ACCURACY]floatValue]<=[[myBestLocation objectForKey:ACCURACY]floatValue]){
myBestLocation = currentLocation;
}
}
}
NSLog(@"My Best location:%@",myBestLocation);
NSLog(@"latitude %@",[myBestLocation valueForKey:@"latitude"]);
NSLog(@"longitude %@",[myBestLocation valueForKey:@"longitude"]);
self.DICT=[NSDictionary dictionaryWithDictionary:myBestLocation];
//If the array is 0, get the last location
//Sometimes due to network issue or unknown reason, you could not get the location during that period, the best you can do is sending the last known location to the server
if(self.shareModel.myLocationArray.count==0)
{
NSLog(@"Unable to get location, use the last known location");
self.myLocation=self.myLastLocation;
self.myLocationAccuracy=self.myLastLocationAccuracy;
}else{
CLLocationCoordinate2D theBestLocation;
theBestLocation.latitude =[[myBestLocation objectForKey:LATITUDE]floatValue];
theBestLocation.longitude =[[myBestLocation objectForKey:LONGITUDE]floatValue];
self.myLocation=theBestLocation;
self.myLocationAccuracy =[[myBestLocation objectForKey:ACCURACY]floatValue];
}
NSLog(@"Send to Server: Latitude(%f) Longitude(%f) Accuracy(%f)",self.myLocation.latitude, self.myLocation.longitude,self.myLocationAccuracy);
//TODO: Your code to send the self.myLocation and self.myLocationAccuracy to your server
//After sending the location to the server successful, remember to clear the current array with the following code. It is to make sure that you clear up old location in the array and add the new locations from locationManager
[self.shareModel.myLocationArray removeAllObjects];
self.shareModel.myLocationArray = nil;
self.shareModel.myLocationArray = [[NSMutableArray alloc]init];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
self.locationActual = [[CLLocation alloc]initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
//CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
// __block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:@"%@ %@",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:@"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:returnAddress delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
}
我在这里犯了什么错误。
任何人都可以帮助消除这种困惑。
提前致谢。
转到 iOS 模拟器 -> 调试 -> 位置 -> 自定义位置...并输入您的 long/lat 坐标。
在模拟器中无法获取基于 GPS 的实际数据,因此您需要使用您的经纬度进行模拟,您可以通过
进行设置
Debug -> Location -> Custom Location
并在那里设置您的值。
可能你是在模拟器中模拟位置。在 xCode 的控制台面板中有一个箭头按钮。单击该按钮和 select "Don't simulate location"。参考图片。
如果这不能解决问题,那么 运行 应用程序。如图所示,进入模拟器的调试菜单并选择 "Custom location" 选项。
您确定在完成块后使用 return 地址吗?我已经使用了上面的代码并且它工作正常。
在这里你可以下载sample code
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:12.92243 longitude:80.23893];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:@"%@ %@",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:@"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:returnAddress delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
//If you try to access retunAddress here you will get nil.
输出
我是 MapView 新手 topic.Now 我正在处理地图 view.I 正在获取旧金山位置的经度和纬度 values.I 正在 simulator.It 进行测试未显示当前位置的经度和纬度值。
借助本教程http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/
我正在开发应用程序。
在 AppDelegate 文件中我写了如下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView * alert;
//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
} else{
self.locationTracker = [[LocationTracker alloc]init];
[self.locationTracker startLocationTracking];
//Send the best location to server every 60 seconds
//You may adjust the time interval depends on the need of your app.
NSTimeInterval time = 60.0;
self.locationUpdateTimer =
[NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:@selector(updateLocation)
userInfo:nil
repeats:YES];
}
return YES;
}
我在 ViewController
中导入了位置追踪器 class我写了下面的代码来获取位置名称和地址
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
[geoCoder reverseGeocodeLocation:self.appDelgate.locationTracker.myLastLocation_ completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:@"%@ %@",placemark.subLocality,placemark.subAdministrativeArea];
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:@"address"];
}
}];
现在我的问题是它没有进入 block.So 我得到 "returnAddress" 作为(空)。
虽然没来,我也是这么写的
- (void)updateLocationToServer {
NSLog(@"updateLocationToServer");
// Find the best location from the array based on accuracy
NSMutableDictionary * myBestLocation = [[NSMutableDictionary alloc]init];
for(int i=0;i<self.shareModel.myLocationArray.count;i++){
NSMutableDictionary * currentLocation = [self.shareModel.myLocationArray objectAtIndex:i];
if(i==0)
myBestLocation = currentLocation;
else{
if([[currentLocation objectForKey:ACCURACY]floatValue]<=[[myBestLocation objectForKey:ACCURACY]floatValue]){
myBestLocation = currentLocation;
}
}
}
NSLog(@"My Best location:%@",myBestLocation);
NSLog(@"latitude %@",[myBestLocation valueForKey:@"latitude"]);
NSLog(@"longitude %@",[myBestLocation valueForKey:@"longitude"]);
self.DICT=[NSDictionary dictionaryWithDictionary:myBestLocation];
//If the array is 0, get the last location
//Sometimes due to network issue or unknown reason, you could not get the location during that period, the best you can do is sending the last known location to the server
if(self.shareModel.myLocationArray.count==0)
{
NSLog(@"Unable to get location, use the last known location");
self.myLocation=self.myLastLocation;
self.myLocationAccuracy=self.myLastLocationAccuracy;
}else{
CLLocationCoordinate2D theBestLocation;
theBestLocation.latitude =[[myBestLocation objectForKey:LATITUDE]floatValue];
theBestLocation.longitude =[[myBestLocation objectForKey:LONGITUDE]floatValue];
self.myLocation=theBestLocation;
self.myLocationAccuracy =[[myBestLocation objectForKey:ACCURACY]floatValue];
}
NSLog(@"Send to Server: Latitude(%f) Longitude(%f) Accuracy(%f)",self.myLocation.latitude, self.myLocation.longitude,self.myLocationAccuracy);
//TODO: Your code to send the self.myLocation and self.myLocationAccuracy to your server
//After sending the location to the server successful, remember to clear the current array with the following code. It is to make sure that you clear up old location in the array and add the new locations from locationManager
[self.shareModel.myLocationArray removeAllObjects];
self.shareModel.myLocationArray = nil;
self.shareModel.myLocationArray = [[NSMutableArray alloc]init];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
self.locationActual = [[CLLocation alloc]initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
//CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
// __block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:@"%@ %@",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:@"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:returnAddress delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
}
我在这里犯了什么错误。 任何人都可以帮助消除这种困惑。 提前致谢。
转到 iOS 模拟器 -> 调试 -> 位置 -> 自定义位置...并输入您的 long/lat 坐标。
在模拟器中无法获取基于 GPS 的实际数据,因此您需要使用您的经纬度进行模拟,您可以通过
进行设置Debug -> Location -> Custom Location
并在那里设置您的值。
可能你是在模拟器中模拟位置。在 xCode 的控制台面板中有一个箭头按钮。单击该按钮和 select "Don't simulate location"。参考图片。
如果这不能解决问题,那么 运行 应用程序。如图所示,进入模拟器的调试菜单并选择 "Custom location" 选项。
您确定在完成块后使用 return 地址吗?我已经使用了上面的代码并且它工作正常。
在这里你可以下载sample code
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:12.92243 longitude:80.23893];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:@"%@ %@",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:@"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:returnAddress delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
//If you try to access retunAddress here you will get nil.
输出