Alright, I've looked high and low for an answer to this. Why can I query the mapview.annotations array for coordinate data but I cant save the same annotation data at generation into an array that is query-able in the same fashion.
This is my code:
- (void)retrieveData {
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://PRIVATE.com/api/cprapi.php?function=request&uid=PRIVATE"]];
NSLog(@"%@",url);
NSData * data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
workzoneArray = [[NSMutableArray alloc]init];
for (int i = 0; i < json.count; i++) {
//Create workzone objects
NSString * sT = [[json objectAtIndex:i]objectForKey:@"startTime"];
NSString * eT = [[json objectAtIndex:i]objectForKey:@"endTime"];
NSString * sLat = [[json objectAtIndex:i]objectForKey:@"startLat"];
NSString * sLon = [[json objectAtIndex:i]objectForKey:@"startLon"];
NSString * eLat = [[json objectAtIndex:i]objectForKey:@"endLat"];
NSString * eLon = [[json objectAtIndex:i]objectForKey:@"endLon"];
NSString * A = [[json objectAtIndex:i]objectForKey:@"active"];
NSString * DUID = [[json objectAtIndex:i]objectForKey:@"deviceUID"];
LTB * workzone = [[LTB alloc] initWithStartTime:sT andEndTime:eT andStartLat:sLat andStartLon:sLon andEndLat:eLat andEndLon:eLon andActive:A andDeviceUID:DUID];
[workzoneArray addObject:workzone];
NSLog(@"workzoneArray: %@", workzoneArray);
}
for (int i = 0; i < workzoneArray.count; i++) {
LTB * workzone = [workzoneArray objectAtIndex:i];
CLLocationCoordinate2D thiscoordinate;
CLLocationCoordinate2D thatcoordinate;
float sLat = [workzone.startLat floatValue];
float sLon = [workzone.startLon floatValue];
NSLog(@"Start: %@ %@",workzone.startLat,workzone.startLon);
float eLat = [workzone.endLat floatValue];
float eLon = [workzone.endLon floatValue];
NSLog(@"End: %@ %@",workzone.endLat,workzone.endLon);
thiscoordinate.latitude = sLat;
thiscoordinate.longitude = sLon;
thatcoordinate.latitude = eLat;
thatcoordinate.longitude = eLon;
myAnnotation *annotation1 = [[myAnnotation alloc] initWithCoordinate:thiscoordinate title:@"Limit"];
[self.mapView addAnnotation:annotation1];
[AnnotationArray addObject:annotation1];
myAnnotation * annotation2 = [[myAnnotation alloc] initWithCoordinate:thatcoordinate title:@"Limit"];
[self.mapView addAnnotation:annotation2];
[AnnotationArray addObject:annotation2];
NSLog(@"%@", AnnotationArray);
NSLog(@"%@", [AnnotationArray objectAtIndex:0]);
NSLog(@"%@", [AnnotationArray objectAtIndex:1]);
}
}
and the code that handles comparisons between my points where I am attempting to pull data from my array.
-(void) mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"checking distances...");
myAnnotation * annotation = [AnnotationArray objectAtIndex:0];
CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
NSLog(@"%f - %f", annotation.coordinate.latitude, annotation.coordinate.longitude);
annotation = [AnnotationArray objectAtIndex:1];
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
NSLog(@"%f - %f", annotation.coordinate.latitude, annotation.coordinate.longitude);
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance dist = [loc1 distanceFromLocation:loc2];
float distance = dist;
NSLog(@"%f", distance);
if (distance < 50) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Limit Alert"
message:@"50 Meters to Limit"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
}
If I objectAtIndex the self.mapView.annotations I get the correct lat and lon, so I'm guessing it's an invalid type conversion as it goes into the array am I right in thinking that?
Is there a better way I could do that? I only am looking at using the array as the mapView.annotations is unreliable based on network speed to which annotated view is at which index with out alot more code.
The goal is to compare loc1 and loc2 get the distance between them. compare current location's distance to loc1 add the current location's distance to loc2 and if within a margin of error check that it is less or the same. But I cant do this until I figure out a reliable way of knowing which mapView.annotation is which.
Maybe I'm blind. Thanks!
ANSWER:
Looked through my code again, decided to use my earlier array which I fed my JSON into and then extract data from there as that will allow it to be extended further down the road. Here's the code I wound up with and it seems to work ok.
NSLog(@"checking distances...");
LTB * workzone = [workzoneArray objectAtIndex:0];
CLLocationCoordinate2D startCoord;
CLLocationCoordinate2D endCoord;
double sLat = [workzone.startLat doubleValue];
double sLon = [workzone.startLon doubleValue];
startCoord.latitude = sLat;
startCoord.longitude = sLon;
double eLat = [workzone.endLat doubleValue];
double eLon = [workzone.endLon doubleValue];
endCoord.latitude =eLat;
endCoord.longitude = eLon;
CLLocation *loc = [[CLLocation alloc] initWithLatitude:startCoord.latitude longitude:startCoord.longitude];
NSLog(@"%f - %f", startCoord.latitude, startCoord.longitude);
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:endCoord.latitude longitude:endCoord.longitude];
NSLog(@"%f - %f",endCoord.latitude, endCoord.longitude);
CLLocation *myloc = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance area = [loc distanceFromLocation:loc1];
NSLog(@"area: %f",area);
CLLocationDistance distToA = [myloc distanceFromLocation:loc];
NSLog(@"distToA %f",distToA);
CLLocationDistance distToB = [myloc distanceFromLocation:loc1];
NSLog(@"distToB %f",distToB);
double outOfBounds = ((distToA+distToB) - area);
NSLog(@"%f", outOfBounds);
if (outOfBounds < (area + 50)) {
if (workzoneAlert == TRUE) {
workzoneAlert = false;
}
if (distToA < 10) {
if (workzoneAlertA == false) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Limit Alert"
message:@"10 Meters to Limit A"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
//set variable so it only shows once
self.workzoneAlertA = true;
}
else {
}
}
else if (distToB <10) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Limit Alert"
message:@"10 Meters to Limit B"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
}
else {
if (workzoneAlert == FALSE){
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Limit Alert"
message:@"Exceeded WorkZone"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
workzoneAlert = TRUE;
}
}
}