iOS - if user allow using his current location or not - iphone

in my app in map view i want to show the nearest 10 stores for the user's current location
but first i have to take current location first then i can show the stores according to user's location
in first start of the app the app ask user if he allow to get current location or not so i must do something like
if user allow
list stores on map
else
go back to main page
now i am using the code below:
mtMap.showsUserLocation=YES;
mymanager=[[CLLocationManager alloc] init];
mymanager.delegate=self;
CLLocation *location = [mymanager location];
CLLocationCoordinate2D coordinate2 = [location coordinate];
NSString *latitude1 = [NSString stringWithFormat:#"%f", coordinate2.latitude];
NSString *longitude1 = [NSString stringWithFormat:#"%f", coordinate2.longitude];
NSString *myURL = [[NSString alloc] initWithFormat:#"http://www.xxxx.com/xxxx/aaaaa.ashx?term=%#,%#",latitude1,longitude1];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:myURL]];
NSInputStream *dataStream=[[NSInputStream alloc]initWithData:data];
[dataStream open];
if(dataStream)
{
NSError *err=nil;
id jsonobject=[NSJSONSerialization JSONObjectWithStream:dataStream options:NSJSONReadingAllowFragments error:&err];
if([jsonobject respondsToSelector:#selector(objectForKey:)])
{
//fill arr
}
}
but it doesn't work for the first time when the user opens the app because of late allowing or getting current location late i can't reach where he is so i can't show nearest places
there can be a problem with my logic. i mean may be i shouldn't do all the job in viewDidload
so could anyone help how can i solve this problem?

Instead of doing it under the viewDidLoad section, what about inserting the code in this method?
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocation *location = newLocation;
...
That would make the code run once the user's location was updated.
You could also add an if statement inside the method to check if it is the first time the code has been run and if not to return.

Related

User current Location on a mapkit in xcode

I need a good tutorial in which user's current location (latitude,longitude,city,state,country) is found and updated from time to time as the location changes and displayed on the map kit with the blue icon zooming.
I did it by placing an MKMapView on view.xib and it shows current location of user (default on simulator :SanFransisco) with blue dot zooming only for the first time. But when I run the app next time it is not showing any blue dot zooming. Should I write any code? Till now I didn't write any code. Just placed a mapkit with Show UserLocation checked in xib. How can I get a blue dot ?
I also need to find nearby doctors from the userlocation and display in the same map with red coloured markers pointing.
Gone through google but confused a lot. Please suggest to me some good tutorials in this regard.
EDIT:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapView setShowsUserLocation:YES];
[locationManager startUpdatingLocation];
[self queryGooglePlaces:#"doctor"];
}
-(void) queryGooglePlaces: (NSString *) googleType {
// https://developers.google.com/maps/documentation/places/#Authentication
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=300&types=doctor&sensor=true&key=%#", coord.latitude, coord.longitude,kGOOGLE_API_KEY];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
//Here I'm getting array data as null because latitude and longitude are passed as 0.000 ..
How can I get both of them on viewDidLoad?
if you want to find the near doctor address you need to use "GOOGLE PLACE API"
//For user current location
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
Check this link its good example of google place api
Add this Delegate method of ccl location manager
.h file
double currentLatitude;
double currentLongitude;
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
currentLatitude = newLocation.coordinate.latitude;
currentLongitude = newLocation.coordinate.longitude;
[self queryGooglePlaces:somestring];
}
-(void) queryGooglePlaces: (NSString *) googleType {
// https://developers.google.com/maps/documentation/places/#Authentication
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=300&types=doctor&sensor=true&key=%#", currentLatitude , currentLongitude,kGOOGLE_API_KEY];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
pass the current latitude and longitude in your -(void) queryGooglePlaces: (NSString *) googleType { } method

iphone : using Background location services is stopped after a little time

i'm trying to get a location and compare it with a location online all that happen in background
the method i create is working fine using background location service but after a minute or so the location icon in the status bar is getting disappear and the method is not getting called any longer
here is the code
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
double lat = newLocation.coordinate.latitude;
double lon = newLocation.coordinate.longitude;
NSURL * locationURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://somedomainname.com/iphoneLocation?lat=%f&lon=%f",lat,lon]];
NSData * responseData = [NSData dataWithContentsOfURL:locationURL];
NSString* aStr;
aStr = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
if ([aStr isEqualToString:#"out Of Any Knowen Range"] ){
UILocalNotification *notify =[[UILocalNotification alloc] init];
notify.alertAction = [[NSString alloc] initWithString: #"View"];
notify.fireDate=nil;
notify.alertBody = [NSString stringWithFormat:#"New Data Occured"];
notify.soundName = UILocalNotificationDefaultSoundName;
NSLog(#"Local notification should display");
[[UIApplication sharedApplication] presentLocalNotificationNow:notify];
}
}
and in the viewDid load i'm using something like this
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
CLLocation *userLocation = [locationManager location];
so what is the wrong with it
You need to modify your AppName-Info.plist file by adding a key Required background modes with an item with value App registers for location updates. Another thing I think you should do as you connect online and this may not happen very fast, so the operation that you connect online, post the location and wait for response should start in another thread and meanwhile if you have received another location from CLLocationManager and your previous request has not finished yet to skip posting the new location...
I wonder if the Location Manager is getting released somewhere and therefore not sending any more updates.
Have you tried setting the location manager to a retained property in your view controller?
#property (nonatomic, strong) CLLocationManager *locationManager

Sending location to server with CLLocationManager when iphone is in background

i'm having trouble sending my position when the application lies in the background. I'm using CLLocationManager and startMonitoringSignificantLocationChanges. The posision didUpdateToLocation delegate method is performed once, but not more. I've tried to walk around but no new locations is sent to the server.
I have set the "Required background modes" -> "App registers for location updates" in the info.plist file.
Anyone got an idea on what might be wrong?
Code from where the tracking is started:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = appDelegate;
[appDelegate setLocationManager:locationManager withDistanceFilter:kCLDistanceFilterNone];
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];
Code (from CLLocationManagerDelegate):
- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter {
// create a new manager and start checking for sig changes
self.theLocationManager.delegate = nil;
[theLocationManager release];
self.theLocationManager = locationManager;
self.theLocationManager.delegate = self;
self.theLocationManager.distanceFilter = distanceFilter;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *newLocationTimestamp = newLocation.timestamp;
NSDate *oldLocationTimestamp = oldLocation.timestamp;
int locationUpdateInterval = 15;//15 sec
if (!([newLocationTimestamp timeIntervalSinceDate:oldLocationTimestamp] < locationUpdateInterval)) {
//NSLog(#"New Location: %#", newLocation);
[self updateToLocation:newLocation];
}
}
- (void)updateToLocation:(CLLocation *)newLocation {
NSLog(#"update location!!");
NSString *latitude = [NSString stringWithFormat:#"%f", [newLocation coordinate].latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", [newLocation coordinate].longitude];
[currentUser updatePositionWithLongitude:longitude andLatitude:latitude];
}
Like Bill Brasky said, the accuracy to which you have set your location manager is likely not registering the distance that you have walked. Try setting your location manager accuracy much higher, just to see if works, then dial it back down to a happy medium between accuracy and battery efficiency. Just for testing, take it all the way up:
[appDelegate.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
Then instead of:
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];
try:
[appDelegate.theLocationManager startUpdatingLocation];
The -startMonitoringForSignificantLocationChanges is directly tied to cell tower connectivity. You may need to travel miles to get connection to a new tower and trigger a location change event. I know that the region monitoring is a bit more accurate as it uses updates of location from Wifi, cell tower, and even other apps that inquire on location. You will need to figure out how accurate and how often you need your app to be. You may need to actively monitor location in the background (which would be a battery killer for sure). Hope this helps.

CLLocationManagerDelegate, when will the method be invoke: locationManager:didUpdateToLocation:fromLocation:

in CLLocationManagerDelegate, when will the method be invoke: locationManager:didUpdateToLocation:fromLocation:
could you explain more detail, if possible could you explain with example? thanks very much
if my current location is changing(eg, I am on a train), does this method be invoked? if yes, how many times or how frequently it is be invoked?
if I stay at one place and not move, does this method be invoked? if yes, how many times or how frequently it is be invoked?
This method is called whenever your iOS device has moved past the distance filter you have set. For example if you set it to
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
The method will be called every time the device is moved.
A code example of this would be finding the coordinates then assigning those values to labels
- (void)viewDidLoad{
[super viewDidLoad];
altitudeLabel.text = #"0 ft";
ftOrM = YES;
// Note: we are using Core Location directly to get the user location updates.
// We could normally use MKMapView's user location update delegation but this does not work in
// the background. Plus we want "kCLLocationAccuracyBestForNavigation" which gives us a better accuracy.
//
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // Tells the location manager to send updates to this object
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation {
tLatitude = [NSString stringWithFormat:#"%3.5f", newLocation.coordinate.latitude];
tLongitude = [NSString stringWithFormat:#"%3.5f", newLocation.coordinate.longitude];
/* the following returns 0 */
float distanceMeters;
float distanceFeet;
if (ftOrM == YES) {
distanceMeters = newLocation.altitude;
distanceFeet = distanceMeters * 3.2808399;
tAltitude = [NSString stringWithFormat:#"%.02f ft", distanceFeet];
altitudeLabel.text = tAltitude;
}
else {
tAltitude = [NSString stringWithFormat:#"%.02f m", newLocation.altitude];
NSLog(#"Altitude:");
NSLog(#"%#", tAltitude);
altitudeLabel.text = tAltitude;
NSLog(#"Altitude:");
NSLog(#"%#", tAltitude);
}
//[manager stopUpdatingLocation];
}
For the first one this method will invoked, and the frequency depends on the speed.
And if don't change your location then this method will not invoked.
And how much time your location is changing this method will call.

Returning a users lat lng as a string iPhone

Is there a way to return the users location as a string from a model?
I have a model thats job is to download same JSON data from a web service. When sending in my request I need to add ?lat=LAT_HERE&lng=LNG_HERE to the end of the string.
I have seen tons of examples using the map or constantly updating a label. But I cant find out how to explicitly return the lat and lng values.
Im only 2 days into iPhone dev so go easy on me :)
You need to leverage Core Location, specifically CLLocationManager. Apple doesn't provide any CL programming guide, so just look at one of the samples like LocateMe to see how to do it.
You need to use CLLocationManager like this:
- (void)viewDidLoad
{
// this creates the CCLocationManager that will find your current location
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
}
// this delegate is called when the app successfully finds your current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// retrieve lat and lng in a string from newLocation.coordinate
NSString *lat = [NSString stringWithFormat:#"%d", newLocation.coordinate.latitude];
NSString *lng = [NSString stringWithFormat:#"%d", newLocation.coordinate.longitude];
}
// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"locationManager:%# didFailWithError:%#", manager, error);
}