Reputation: 5
I am new to the MapView topic.Now I am working on the map view.I am getting the san Francisco location longitude and latitude values.I am testing in the simulator.It is not showing the current location longitude and latitude values.
With the help of this tutorial http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/
I am developing the app.
In AppDelegate file I wrote the following code like this
- (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;
}
I have imported Location Tracker class in my ViewController
and I wrote the following code to get the location name and addresss
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"];
}
}];
Now my problem is that it is not going inside block.So that I am getting the "returnAddress" as (null).
I wrote like this even though it is not coming
- (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];
}
}];
}
What mistake i have done here. Can anyone please help to clear this confusion. Thanks in Advance.
Upvotes: 0
Views: 116
Reputation: 3529
Are you sure you are using the return address after completion block? I have used the above code and its working fine.
Here you can download the 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.
OUTPUT
Upvotes: 1
Reputation: 2496
Probably you are simulating the location in the simulator. In your xCode's console panel there is an arrow button. Click that button and select "Don't simulate location". For reference, see the image.
If that doesn't resolve the problem then run the application. Go into the Debug menu of the simulator and choose the "Custom location" option as shown in the image.
Upvotes: 0
Reputation: 4005
In the simulator there is no way to get the actual GPS based data so you need to simulate is with your lat-long which you can set by going
Debug -> Location -> Custom Location
and set your values there.
Upvotes: 0
Reputation: 356
Go to iOS Simulator -> Debug -> Location -> Custom location... And enter your long/lat coordinates.
Upvotes: 0