CoreLocation start Monitoring For multi Regions - ios

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

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

Create MKPolyline with only recent CLLocations

My app loads the user's past polylines and displays them on the map. Then the app starts tracking and a straight line is drawn from the last updated coordinate to the first, thereby connecting the two separate lines when they should be separate (shown here
I want to remove this straight line, so I'm thinking the easiest way would be to discard coordinates that are outside a set time frame (e.g 1 minute), so polylines on the map remain separate. I'm not sure how to do this though... I'm a beginner so any suggestions would be much appreciated!
I use this code when loading the past polyline
(IBAction)didClickLoadCoordinates:(id)sender {
// get a reference to the appDelegate so you can access the global managedObjectContext
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:#"Route"];
NSError *error;
id results = [appDelegate.managedObjectContext executeFetchRequest:request error:&error];
if ([results count]) {
polyLine = (Route *)(results[0]);
NSArray *coordinates = polyLine.coordinates;
int ct = 0;
for (CLLocation *loc in coordinates) {
NSLog(#"location %d: %#", ct++, loc);
}
// this copies the array to your mutableArray
_locationsArray = [coordinates mutableCopy];
}
NSInteger numberOfSteps = _locationsArray.count;
//convert to coordinates array to construct the polyline
CLLocationCoordinate2D clCoordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_locationsArray objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
clCoordinates[index] = coordinate2;
}
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:clCoordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
And this code to just normally update the cllocations and make the polyline
//get the latest location
CLLocation *currentLocation = [locations lastObject];
//get latest location coordinates
CLLocationDegrees latitude = currentLocation.coordinate.latitude;
CLLocationDegrees longitude = currentLocation.coordinate.longitude;
CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(latitude, longitude);
//zoom map to show users location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 2000, 2000);
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];
//store latest location in stored track array
[_locationsArray addObject:currentLocation];
//create cllocationcoordinates to use for construction of polyline
NSInteger numberOfSteps = _locationsArray.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_locationsArray objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
coordinates[index] = coordinate2;
}
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[_mapView addOverlay:routeLine];
NSLog(#"%#", _locationsArray);

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

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

Resources