needs to calculate distance between two location [duplicate] - ios

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

Related

showing the locations and finding the nearest spot in ios

I want to display my spots NSArray *chSpot = [parser parseArray:responseObject];
which receives 3 objects , I want to show each location regarding to its lat and long
and find the nearest spot according to my location...I am using this code:
NSArray *chSpot = [parser parseArray:responseObject];
// GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[locLat doubleValue]
// longitude:[locLong doubleValue]
// zoom:4];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:nil];
mapView_.myLocationEnabled = YES;
mapView_.settings.myLocationButton = YES;
for (int i=0; i<chSpot.count; i++)
{
ChargingSpots *spot = [chSpot objectAtIndex:i];
NSString *locLat = spot.LocationLat;
NSString *locLong = spot.LocationLong;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([locLat doubleValue], [locLong doubleValue]);
marker.title = #"Amman";
marker.snippet = #"Jordan";
mapView_.delegate=self;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [UIImage imageNamed:#"car"];
marker.map = mapView_;
}
//[mapView_ setSelectedMarker:marker];
self.view = mapView_;
//
//
// CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]];
//
// CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]];
//
// CLLocationDistance distance = [ locationA distanceFromLocation:locationB];
//
// NSLog (#"%f",distance);
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];
In your locationManager's delegate's method locationManager:didUpdateLocations: you can get your current location and it provides the method to calculate distanceFromLocation:(const CLLocation *)location
CLLocationDistance distA = [currentLocation distanceFromLocation: locationA];
CLLocationDistance distB = [currentLocation distanceFromLocation: locationB]; //etc

Why is the distanceFromLocation: method returning a ridiculous number

I'm trying to calculate and return the distance between two points and I'm getting a ridiculous number. I've tried both a user-defined haversine method and the distanceFromLocation method and they are both returning 10282657.652... for the distance between lat = 32.60641072/ lon = -85.48713530 and lat = 32.605638 and lon = -85.486890. Are these distances too close?
CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.latitude];
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:_head.lattitude longitude:_head.longitude];
double distance = [startLocation distanceFromLocation:endLocation];
CLLocationCoordinate2D newCoordinate = [startLocation coordinate];
CLLocationCoordinate2D oldCoordinate = [endLocation coordinate];
double distance2 = [self distanceFrom:newCoordinate to:oldCoordinate];
NSNumber *myDoubleNumber = [NSNumber numberWithDouble:distance2];
_charLabel.text = [myDoubleNumber stringValue];
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:lon2];
CLLocationDistance distance= [location1 distanceFromLocation:location2]*1.09361;
"distance" will return on meter.
This code worked for me :)

Wrong Distance CLLocation

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];

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);

iOS checking to see if annotation's coordinate is {0,0}

I have a loop statement that displays my annotations on the mapView from an array. How do I check to see if any of the coordinates in the array are 0,0, and if so, remove/dont plot them?
Thanks.
Code:
CLLocationCoordinate2D maxCoord = {45.60250f,-122.39181f};
CLLocationCoordinate2D minCoord = {45.35697f,-123.12789f};
NSArray *callsArray = [xmlParser calls];
for (JointCAD *call in callsArray) {
NSString *callnumber = [call.callnumber stringByAppendingFormat:#". "];
NSString *callandnumber = [callnumber stringByAppendingString:call.currentCallType];
CLLocationCoordinate2D newCoord = { [call.latitude doubleValue], [call.longitude doubleValue]};
if ([call.longitude doubleValue] > maxCoord.longitude)
{
maxCoord.longitude = [call.longitude doubleValue];
}
if ([call.latitude doubleValue] > maxCoord.latitude)
{
maxCoord.latitude = [call.latitude doubleValue];
}
if ([call.longitude doubleValue] < minCoord.longitude)
{
minCoord.longitude = [call.longitude doubleValue];
}
if ([call.latitude doubleValue] < minCoord.latitude)
{
minCoord.latitude = [call.latitude doubleValue];
}
Annotation *ann = [[Annotation alloc] init];
ann.title = callandnumber;
ann.subtitle = [call location];
ann.coordinate = newCoord;
[mapView addAnnotation:ann];
}
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
region.span.longitudeDelta = (maxCoord.longitude - minCoord.longitude) * 1.1;
region.span.latitudeDelta = (maxCoord.latitude - minCoord.latitude) * 1.1;
[mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
[self setRefreshState:#"Finished"];
Hm why don't you just add a continue in your for loop when the coordinates are equal 0?
Before creating the Annotation object just add a simple
if(newCoord.latitude == 0 && newCoord.longitude == 0) continue;
continue simply skips to the next iteration of a loop.

Resources