how to avoid calling location did update for three times - iphone

location did update calling three time and location did fail with error not call even when no data receive
how to avoid calling location did update three times and call did fail with error for no data receive?
im not taking about internet connection

How to avoid calling three times location did update?
for ios location update delegate may call several time depend upon on speed not three times
so use bool variable to indicate
How to call did fail with error for no internet connection?
you have set location manger delegate with self

How to avoid calling three times location did update?
Set location manager's delegate to nil in this method.
How to call did fail with error for no internet connection?
Why should it be called? Locations may come from three sources: GPS radio, cellular and WiFi, but not from the internet.

LocationManager didUpdateToLocation always check for change in location. If it fing multiple location it will call multiple time. You can avoid it in different way. But this will not update the location if user changes his current place.
You can user this code just to get location and proceed further.
-(void)getLocation{
locationManager = [[CLLocationManager alloc] init] ;
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// [locationManager startUpdatingLocation]; commented to avoid multiple calling or you can say updating current location
CLLocation *newLocation=[locationManager location];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSString * location = ([placemarks count] > 0) ? [[placemarks objectAtIndex:0] locality] : #"Not Found";
NSLog(#"location:%#",location);
NSLog(#"lat:%f",locationManager.location.coordinate.latitude);
NSLog(#"long:%f",locationManager.location.coordinate.longitude);
}];
}

Related

location monitoring in a iphone app

I am an newbie with iphone app development. I would like to capture location information without using GPS but only with Cell tower info and Wifi data. To enable this I am using the CLLocationManager to capture location information,
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringSignificantLocationChanges];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
float latitude = newLocation.coordinate.latitude;
float longitude = newLocation.coordinate.longitude;
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
// NSTimeInterval is defined as double
NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
NSLog(#"latitude %f and longitude %f", latitude, longitude );
NSLog(#"%d", [timeStampObj integerValue]);
NSMutableArray *numArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithDouble:latitude],[NSNumber numberWithDouble:longitude], timeStampObj, nil];
}
When I use the above code when the app is launched I see that the app requests for accessing location services. Once this is accepted, I am not sure if the app is making use of GPS to get the location information as I see a small arrow pointing out, which seems as though GPS is being used to obtain the location information, which is not what I require. I would want the location information to be captured using WiFi and Cell Tower information only.
Also is there a frequency in which this location information gets updated, as there is an initial update of location after that I do not see the location getting modified even after the phone is being continuously moved.
If you use this:
[locationManager startMonitoringSignificantLocationChanges];
You are only getting GPS uploads when a significant change happens (for example, when you move from a telephony tower to other).
You cannot choose if you want to use GPS or towers from the code.

deferredLocationUpdate iOS 6

I have looked up about 3 different pages on how this works. But, I could really use some help because I am getting kCLError = 15. Here is what I have so far then I will explain more.
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self]
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(#"%#",[locations lastObject];
NSLog(#"%d",[CLLocationManager deferredLocationUpdatesAvailable]);
[locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:CLTimeIntervalMax];
After this I have my error code
-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error
{
NSString *stringError = [NSString stringWithFormat:#"error: %#",[error description]];
_whatMonitor.text = stringError;
}
So, anyone that can help me I will seriously be so thankful. I have a count of the locations array as well, but this never changes from 1.. It is my understanding that after closing app to home screen and locking the device, the deferredUpdates should kick in. I have checked the [locations count] and it is still 1. I am expecting it to be greater than this..
I do not claim to be very good at this, so if I am making a careless mistake please let me know. I did not copy and paste so there may be some small typos. Thanks in advance.
I am running iOS 6.0 on an iPhone 5.
For significant change service and deferred location updates you have to move for the system to record an update. You have specified CLLocationDistanceMax which is a high value for distance. You can specify a lower distance to get more frequent changes for example you could specify every 100 meter change to trigger an update as follows:
[locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocation(100) timeout:CLTimeIntervalMax];
Reference: http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html

CLGeocoder reverseGeocodeLocation "kCLErrorDomain error 2"

I'm developing an iOS app with reverse geocoding features. When I call the function the first time everything is fine. After the second call (with a new instance of the controller where the call is done) the "Domain=kCLErrorDomain Code=2" Error appears. This happens on the Simulator and on the device. The Coordinates are valid.
My Code:
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:cityCoords.latitude longitude:cityCoords.longitude];
self.displayedCity = [[Stadt alloc] init];
[geoCoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
if(!error){
for (CLPlacemark * placemark in placemarks) {
self.displayedCity.name = [placemark locality];
self.displayedCity.stadtCoord = placemark.region.center;
}
[self loadCity:self.displayedCity.name];
}
else{
NSLog(#"failed getting city: %#", [error description]);
}
}];
Thanks in advance!!
Error 2 usually means that you have called the geolocation server too often. Typically this happens when you send a reverse-Geocoding Request to the server each time the delegate method didUpdateLocations was fired. In the docs Apple says that this should typically only be done once a minute.
More info on the this error can be found in Apple's docs on kCLErrorDomain: Core Location Constants Reference and in CLError.h:
kCLErrorNetwork The network was unavailable or a network error occurred.

Userlocation updated to wifi every 10 seconds

I am trying to updated the userlocation every 10 secs from my code. But when I move from wifi to 3G then I face an issue. The every alternate request goes from the location where my wifi is located. What can be done?
Here is my code
NSNumber *n1;
NSNumber *n2;
if([ud boolForKey:#"applicationActive"])
{
n1 = [NSNumber numberWithDouble:[map.userLocation coordinate].latitude];
n2 = [NSNumber numberWithDouble:[map.userLocation coordinate].longitude];
}
else
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
n1 = [NSNumber numberWithDouble:locationManager.location.coordinate.latitude];
n2 = [NSNumber numberWithDouble:locationManager.location.coordinate.longitude];
[locationManager stopUpdatingLocation];
}
[dict setObject:[ud objectForKey:#"CurrentDriverID"] forKey:#"DriverId"];
[dict setObject:[n1 stringValue] forKey:#"Latitude"];
[dict setObject:[n2 stringValue] forKey:#"Longitude"];
[requestG SaveDriverlocationWS:dict delegate:self];
NOTE: The application stays in foreground when this happens. The SaveDriverlocationWS method saves the current location to the server
You don't seem to be setting an accuracy level for your location manager - I would try this and see if things improve. It looks from your code like you might be writing an app for drivers, in which case you may want to use the kCLLocationAccuracyBestForNavigation accuracy level (if you, you should use another more appropriate level, since the BestForNavigation filter uses up a lot of power and is designed for situations where the phone is in a car dock and powered).
For my understanding. using Wifi or 3G is system level setting. CLLocationManager uses current system's network to get device's location, while programmer has nowhere to switch network between 3G/Wifi.

Can't get data from Google Places

I am trying to get the google places API to work on my iPhone project. Now, I had it working about an hour ago, but I can't seem to figure out what I did to make it stop working. Any help would be appreciated.
Here is what I have so far:
- (NSString *)searchString {
// this mutable string allows me to dynamically create the search string
// we start with the static part of the api search URL
NSMutableString *result = [NSMutableString stringWithString:#"https://maps.googleapis.com/maps/api/place/search/json?location="];
// since I need to get the user's location, I need to create a location manager
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
// we need to now update the current location,
// otherwise there will be no coordinates
[locationManager startUpdatingLocation];
// now that it's updated, we stop it because I
// am not tracking anything
[locationManager stopUpdatingLocation];
// this appends the lattitude/longitude, as double values, into the URL
[result appendFormat:#"%g,%g", [[locationManager location] coordinate].latitude, [[locationManager location] coordinate].longitude];
// release the location manager for memory management
[locationManager release];
// if a filter is present, add the keyword item to try to filter
// the results
if([[self filterString] length] > 0) {
[result appendFormat:#"&keyword=%#", filterString];
}
// add the rest of the validated URL now
//[result appendString:#"&types=food|meal_delivery|meal_takeaway|restaurant&rankby=distance&sensor=true&key=AIzaSyBmO_f6h4_Q0xArw6tdxUF7TH7rZpaiFfQ"];
[result appendString:#"&types=food&rankby=distance&sensor=true&key=mykey"];
// log the result for testing
NSLog(#"Completed Search String: %#", result);
return result;
}
Now, when I look at my log, copy the 'completed search string' into Safari, it brings up the results that I need.
But if I use the following code, the app hangs:
- (void)performSearch {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self searchString]]]; // hangs on this line!
NSDictionary *jsonDictionary = [data objectFromJSONData];
NSArray *resultsArray = [jsonDictionary objectForKey:#"results"];
currentList = [ARGooglePlace placesWithArray:resultsArray];
[self.tableView reloadData];
}
I think I should mention that I am using the JSONKit to do the JSON parsing. Also, the ARGooglePlace is a custom class that isn't relevant right now (it doesn't even get there...)
Thanks for any help that you can provide.
Pull the location manager and the lat/long out of the searchString method... just put it in the performSearch method. And instead, pass the lat/long as received from location manager into searchString.
It sounds like a timing issue with your location manager. It could have been working earlier b/c location manager had previously cached location data... and was able to grab the correct coords.
2+ Possible scenarios:
1) problem with location manager not updating it's coords and just hanging there
2) google website being the culprit (maybe loading too much data??)
Pulling location manager out of the searchString method will help isolate cause... and one can just pass lat/long values directly to test google website as well.