Wrong Distance CLLocation - ios

In this code I receive a wrong distance, _latitude and _longtitude are NSString.
Any suggestion to correct this?
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coord;
coord.latitude = [_latitude doubleValue];
coord.longitude = [_longitude doubleValue];
CLLocation *anotLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
_distancia = [NSNumber numberWithFloat:([location distanceFromLocation:anotLocation]/1000)];

check out this singleton implementation of location controller
https://github.com/hackerinheels/locationcontroller
with this just call
CLLocation *currLocation = [[LocationController sharedLocationInterface]getCurrentLocation];

Related

needs to calculate distance between two location [duplicate]

This question already has answers here:
Calculate distance between two place using latitude-longitude in gmap for iPhone [duplicate]
(2 answers)
Closed 8 years ago.
I tried to calculate the distance between two locations but something is wrong because the distance is 0 every time.
float oldLatitude = [gInfo.latitude floatValue];
float oldLongitude = [gInfo.longitude floatValue];
CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:oldLatitude longitude:oldLongitude];
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:newLatitude longitude:newLongitude];
if (newLatitude == 0 && newLongitude == 0) {
[self showAlarm2];
return;
}
float distance = [newLocation distanceFromLocation:oldLocation];
// distance 0 ????????
int result = (int)roundf(distance );
if (result < 0) {
result = -result;
}
// long result = (long)distance;
NSLog(gInfo.name );
NSLog(#"Distance i meters: %i", result);
//**************** filter group by check distance *************
if (result <= 10) {
[viewArray addObject:gInfo];
}
Use this
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
latitude = coordinate.latitude;
longitude = coordinate.longitude;
MKCoordinateRegion myRegion;
MKCoordinateSpan mySpan;
mySpan.latitudeDelta = 1.0f;
mySpan.longitudeDelta = 1.0f;
myRegion.center = coordinate;
myRegion.span = mySpan;
//-----------------------------------------------
NSDictionary *dict = (NSDictionary*)[arrayJsonData objectAtIndex:indexPath.row];
CLLocation *locA = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[dict1 valueForKey:#"Latitude"] doubleValue] longitude:[[dict1 valueForKey:#"Longitude"] doubleValue]];
CLLocationDistance distance = [locA distanceFromLocation:locB];
double distanceInMiles = (distance/CONVERSION_TO_MILES);
// CONVERSION_TO_MILES 1609.344

CoreLocation start Monitoring For multi Regions

how can i start monitoring for multi regions by using locationManager startMonitoringForRegion method
for example i have three regions i want to monitor it
CLLocationCoordinate2D centreLoc = {28.965243, 48.149724};
CLLocationDistance regionRadius = 200.00;
CLRegion *grRegion = [[CLRegion alloc] initCircularRegionWithCenter:centreLoc radius:regionRadius identifier:#"grRegion1"];
CLLocationCoordinate2D centreLoc2 = {28.765243, 48.149724};
CLLocationDistance regionRadius2 = 200.00;
CLRegion *grRegion2 = [[CLRegion alloc] initCircularRegionWithCenter:centreLoc2 radius:regionRadius2 identifier:#"grRegion2"];
CLLocationCoordinate2D centreLoc3 = {28.865243, 48.149724};
CLLocationDistance regionRadius3 = 200.00;
CLRegion *grRegion3 = [[CLRegion alloc] initCircularRegionWithCenter:centreLoc3 radius:regionRadius3 identifier:#"grRegion3"];
CLLocationAccuracy acc2=kCLLocationAccuracyBest;
[locationManager startMonitoringForRegion:grRegion2 desiredAccuracy:acc2];
how can start monitor for this three regions ???
You are on the right track.
First, your coordinates should be a double value, you are using a float value.
Second, you need to make separate arrays for Latitude and Longitude, and then take those values at index i.
This should work.
for (int i = 0; i < [AllRegionsArray count]; i++) {
NSArray *longLati = [allRegionsArray objectAtIndex:i];
NSMutableArray *latArray = longLati objectAtIndex:0];
NSMutableArray *longArray = longLati objectAtIndex:1];
lutiuid = [latArray objectAtIndex:i];
longtuid = [longArray objectAtIndex:i];
CLLocationCoordinate2D centreLoc = [CLLocation2DMake([[lutiuid] doubleValue], [[longtuid] doubleValue]);
//iOS 6
CLRegion *grRegion = [[CLRegion alloc] initWithCenter:centreLoc radius:200 identifier://yourIdentifier, I used the string of the address];
//iOS 7
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:radius identifier:identifier];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringForRegion:grRegion];
NSLog(#"monotoring regions:%#", locationManager.monitoredRegions);

Issue with zooming MapView using current location

I am showing a map view on front page of my application, I set the region and coords from current location using CLLocation.
When the application is run, a blue screen is being displayed on the mapview, when I open its child views and come back to main screen, the map show perfectly with location and zoom level.
Is there any thing wrong in the following code which causes the blue screen?
- (void)viewWillAppear:(BOOL)animated {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
CLLocationCoordinate2D coord;
coord = [location coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
My guess would be that you are seeing blue because the location is (0, 0) which is in the middle of the sea. Probably because the first time round, the location on the CLLocationManager is not set yet.
You should probably check if there's a location yet and if not then wait for a callback in the CLLocationManagerDelegate method locationManager:didUpdateToLocation:fromLocation: and then centre the map on the location you want.
Something like this:
...
#property (nonatomic, assign) BOOL needsCentering
...
#synthesize needsCentering = _needsCentering;
- (void)viewWillAppear:(BOOL)animated {
CLLocationManager *lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
CLLocation *location = [lm location];
if (!location) {
_needsCentering = YES;
} else {
_needsCentering = NO;
CLLocationCoordinate2D coord;
coord = [location coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (_needsCentering) {
_needsCentering = NO;
CLLocationCoordinate2D coord;
coord = [newLocation coordinate];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = coord.latitude; //39.281516;
zoomLocation.longitude= coord.longitude; //-76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];
[self.mainMapView setRegion:adjustedRegion animated:YES];
}
}
But also, you might want to take a look at showsUserLocation of MKMapView.

Calculating Distance iOS MapKit Using CLLocation

I am trying to find the distance in miles using the latitude and longitude. I have the following method:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if(!newLocation) return;
if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
(oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
{
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.latitude];
CLLocationDistance distance = ([loc2 distanceFromLocation:loc1]) * 0.000621371192;
//distance = distance;
NSLog(#"Total Distance %f in miles",distance);
}
}
For some reason it prints out something like 5678.32 miles. The location is pretty much stationary and not moving at all.
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.(!!!!!)latitude longitude:newLocation.coordinate.(!!!!!)latitude];
You are initializing loc2 with the latitude of the new location for both the longitude and the latitude:
This line:
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.latitude];
Should say:
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
Hope it helps!

longitude and latitude values are both 0.000000 , why?

i use core location to get the longitude and latitude value of my current position and i have displayed it into a label, so for my case it's always 0.000000 for both longitude and latitude, is it because i work on the simulator ??
//longitude and latitude
// locationManager update as location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
Just initialization the location manager doesn't fetch you values.
you have to write this
[self.locationManager startUpdatingLocation];
then you have to override a callback method
- (void) locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation {
[self.locationManager stopUpdatingLocation];
NSString *latitude = [NSString stringWithFormat:#"%f", newLocation.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", newLocation.longitude];
[self.locationManager release];
}
Hope that helps!
Shuanak
No. CoreLocation on the iPhone simulator is fixed at 37.3317 North, 122.0307 West (Apple HQ).
Try moving your locationManager code away from your code with the label. Put the location manager code in the viewDidLoad or something and then put this code
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
in a function that you call from a button. CoreLocation needs time to get itself going. This is true on the device for sure, but perhaps it is also true on the simulator.

Resources