Get specific places on a map from CoreLocation - ios

Is there a way to determine specific places that users are currently in? I would like to show more than the current city or country they are in and instead show lakes, parks, and other things that if searched on a map would correspond to that location (i.e. Sears Tower, Downtown New York, Yellowstone Park). Also, if this is not possible can anyone provide an example showing the updated way of getting which city you are in? Im using Obj-c in Xcode, but I am having trouble doing this. Any help would be appreciated!
Here is the code I'm using so far:
Implementaion
CLLocationManager *locationManager;
ViewDidLoad
locationManager = [[CLLocationManager alloc] init];
Button
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
Void Method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(#"%#", currentLocation);
}
}

Pass the coordinates you receive from LocationManager to CLGeocoder
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:
^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0) {
CLPlacemark *placemark = placemarks[0];
NSLog(#"country is: %#", placemark.country );
}
}];

Related

When I fetch location of device, it always shows location set on Edit scheme of Xcode using Corelocation framework in ios

I am checking on device not on simulator and I want to fetch user's current location but it always shows location which I have set in edit scheme field from target of Xcode. I am using below code:
- (void)CurrentLocationIdentifier
{
//---- For getting current gps location
// locationManager = [CLLocationManager new];
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if(kCLAuthorizationStatusDenied)
{
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//currentLocation = [locations objectAtIndex:0];
currentLocation=[locations lastObject];
NSLog(#"location:%#",currentLocation);
[locationManager stopUpdatingLocation];
//[locationManager startUpdatingLocation];
geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
if (!(error))
{
placemark = [placemarks objectAtIndex:0];
NSLog(#"\nCurrent Location Detected\n");
NSString *Country = [[NSString alloc]initWithString:placemark.ISOcountryCode];
NSString *state =[[NSString alloc]initWithString:placemark.administrativeArea];
//NSString *state =placemark.administrativeArea;
NSLog(#"%#",Country);
NSLog(#"administrativeArea>>%#",state);
self.stateLabel.text=state;
//[locationManager stopUpdatingLocation];
[[NSUserDefaults standardUserDefaults]setValue:Country forKey:#"countryISOCode"];
}
else
{
}
}];
[locationManager stopUpdatingLocation];
}
How to fetch exact country and state information of user?

getting different location even if I am not moving

I am using below code for getting longitude and latitude , but it gives me diifferent values even if I am not moving from my place
locationmanager=[[CLLocationManager alloc]init];
geocoder=[[CLGeocoder alloc]init];
locationmanager.delegate=self;
locationmanager.desiredAccuracy=kCLLocationAccuracyBest;
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[locationmanager requestWhenInUseAuthorization];
}
[locationmanager startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"error" message:#"Failed to get Location." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentlocation=newLocation;
if (currentlocation!=nil)
{
longitude=currentlocation.coordinate.longitude;
latitude=currentlocation.coordinate.latitude;
}
[geocoder reverseGeocodeLocation:currentlocation completionHandler:^(NSArray *placemarks, NSError *error) {
} ];
}
please guide me, I want same location, if I am not moving from my place.
Cellular network location gives an approximate area were the phone existing. It is based on the service providers tower.So current location will automatically changes a bit depending on network/gps location tracking/accuracy.
I recommend you to get location using distance filter, so until you are in that region your current address will remain same as location only updates for every X meters.
locationManager.distanceFilter = 100;//Your own distance
i.e.,
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
_locationManager.delegate = self;
// This is the most important property to set for the manager. It ultimately determines how the manager will
// attempt to acquire location and thus, the amount of power that will be consumed.
_locationManager.distanceFilter = 100;
// Once configured, the location manager must be "started".
[_locationManager startUpdatingLocation];
Hope this helps.
In ViewController.h file:
#interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *location;
}
In ViewController.m file:
- (void)viewDidLoad
{
location = [[CLLocationManager alloc] init];
location.delegate = self;
location.distanceFilter = 100;
[location requestWhenInUseAuthorization];
[location requestAlwaysAuthorization];
[location startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *locati = [locations lastObject];
float lat=locati.coordinate.latitude;
float lon=locati.coordinate.longitude;
NSLog(#"%f %f",lat,lon);
[location stopUpdatingLocation];
}
In your info.plist add :

Iphone device getting the user location define in xcode

I am using CLLocationManger to get device current location. All is going well on simulation. I check the "Allow Location Simulation" and set Sydney, Australia as default location. I got the lat and long of Sydney on simulator that is fine
My problem is that when i try on device I still get the Sydney location however my device current location is India.
My device get the location what ever I set in Xcode.
Here is my code:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 1;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
#pragma mark delegate method which tell the current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
latitudeCurrentLocation = [NSString stringWithFormat:#"%f",newLocation.coordinate.latitude];
longitudeCurrentLocation= [NSString stringWithFormat:#"%f",newLocation.coordinate.longitude];
[locationManager stopUpdatingLocation];
locationManager=nil;
NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
Please suggest what should I should do.
Use this delegate method and check your location address
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
Here is sample code :
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:_locationManager.location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"reverseGeocodeLocation:completionHandler: Completion Handler called!");
if (error){
NSLog(#"Geocode failed with error: %#", error);
return;
}
NSLog(#"placemarks=%#",placemarks);
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(#"placemark.ISOcountryCode =%#",placemark.ISOcountryCode);
NSLog(#"placemark.country =%#",placemark.country);
NSLog(#"placemark.postalCode =%#",placemark.postalCode);
NSLog(#"placemark.administrativeArea =%#",placemark.administrativeArea);
NSLog(#"placemark.locality =%#",placemark.locality);
NSLog(#"placemark.subLocality =%#",placemark.subLocality);
NSLog(#"placemark.subThoroughfare =%#",placemark.subThoroughfare);
NSLog(#"stringlocation:%#",_cityString);
}];
_userLocation = [locations lastObject]; // This will give you co-ordinates
[_locationManager stopUpdatingLocation];
}
When you run this in your device select don't simulate location

Core Location Not Finding Current Location

Current location is not being found on initial load or resume after multi-tasking. This is what I have:
ViewController.h
#interface ViewController : UIViewController <CLLocationManagerDelegate, ADBannerViewDelegate, BEMSimpleLineGraphDelegate>
ViewController.m
#interface ViewController (){
CLLocationManager *locationManager;
CLLocation *location;
CLLocationCoordinate2D coordinate;
int timeofday;
NSString *cityName;
NSMutableArray *ArrayOfValues;
}
#end
Further down:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateLabels) name:UIApplicationWillEnterForegroundNotification object:nil];
// set up coordinates for current location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = (id)self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
location = [locationManager location];
}
Then my update labels method (which has other code that updates my view below this):
- (void)updateLabels
{
CLLocationCoordinate2D currentLocation = [location coordinate];
if (currentLocation.latitude) {
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error){
NSLog(#"Geocode failed with error: %#", error);
return;
}
CLPlacemark *placemark = [placemarks objectAtIndex:0];
cityName = [NSString stringWithFormat:#"%# is currently",placemark.locality];
}];
[self updateLabels];
} else {
NSLog(#"Could not find the location.");
}
}
And here are the 2 delegate methods for cclocationmanager:
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
[self updateLabels];
[locationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error){
NSLog(#"Geocode failed with error: %#", error);
return;
}
// CLPlacemark *placemark = [placemarks objectAtIndex:0];
}];
}
Try removing the call to stopUpdatingLocation. Also un-comment the log statement in didUpdateToLocation.
Be aware that the method locationManager:didUpdateToLocation:fromLocation: is deprecated in iOS 6. It might not even be called in iOS 7. You should be using `locationManager:didUpdateLocations:' instead.
Your call to updateLabels is using the value set in the member variable location. However your didUpdateToLocation method is not setting this value.
Put some breakpoints in your code and see where it triggers.
Note, for a weather application you do not need kCLLocationAccuracyBest, that takes way more time than coarser settings. You'll get your answer much quicker.
You are setting location, which I assume is a class level iVar, in ViewDidLoad.
location = [locationManager location];
I expect that the location manager at that point has no location.
Then, in the delegate call, you are updating a method level iVar :
CLLocation *currentLocation = newLocation;
This variable is never used, that I can see. Your updateLabels call once again refers to location which has never been updated.
CLLocationCoordinate2D currentLocation = [location coordinate];
Change the delegate to:
location = newLocation;
As #Duncan C stated, the delegate method you are using is deprecated, you should really be using locationManager:didUpdateLocations: in which case you would use:
location = [locations lastObject];

ReverseGeocodeLocation City not accurate enough

I'm receiving an array of placemarks from :
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
..
}
when printing to console using NSLog, the placemark gives me data in the form of:
..HIDDEN.. St., Brighton, MA 02135, United States #
<+42.HIDDEN,-71.HIDDEN> +/- 100.00m
But when using the following code, I get Boston as the city
[placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey]);
Brighton is a part of Boston, but how can the console log be showing me a more specific location than the City key..?
It looks like the best you can do with placemarks is
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
This post may be of some use iOS: get Accurate Reverse Geocoding using CLLocationManager?
It's also worth noting that using Boston in place of Brighton is still a valid address for the same location.
http://support.apple.com/kb/HT1975
This worked for me:
- (void) resetLocationData{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
NSLog(#"LAT: %f", [locationManager location].coordinate.latitude);
NSString *latitude = [NSString stringWithFormat:#"%f", [locationManager location].coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", [locationManager location].coordinate.longitude];
NSString *latlong = [NSString stringWithFormat:#"%#,%#", latitude, longitude];
NSLog(#"LONG: %f", [locationManager location].coordinate.longitude);
NSLog(#"Combined Location: %#", latlong);
[[locationManager location]timestamp];
}
#pragma mark - CLLOCATION STUFF
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.currentLocation = newLocation;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:self.currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(#"Placemark is %#", placemarks);
CLPlacemark *place = [placemarks objectAtIndex:0];
NSLog(#"placemark 0: %#", place);
// locationLabel.text = placemarks;
}];
if(newLocation.horizontalAccuracy <= 100.0f) {
[locationManager stopUpdatingLocation];
}
}
LOG OUTPUT:
2013-01-13 23:23:05.508 CLLocationTest[47617:907] Placemark is (
"XX XXXXXX Rd, XX XXXXXX Rd, Brighton, MA 02135, United States # <+42.XXXXXX,-71.XXXXXXX> +/- 100.00m"

Resources