0

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;

    }



}


}
Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
  • It's not clear what you're trying to do nor what the problem was. What does "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" mean exactly? What error or warning did you get? –  Dec 27 '13 at 14:34
  • "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.": What does that mean exactly? If you were assuming the mapView.annotations array is in a specific order, please see this: http://stackoverflow.com/questions/9539802/mkmapview-annotations-changing-losing-order –  Dec 27 '13 at 14:35
  • I'm sorry you didn't understand what I was saying, let me try rephrasing it. If I pull the data from mapView.annotations array I was getting the correct lat and lon that I had put into the original annotation. When I did the same from my AnnotationArray I got back 0.000000000 for my lats and lons, I could verify that the data went into that array when the annotation was generated but I couldn't seem to pull it out again. – user3138529 Dec 28 '13 at 17:56
  • The link you posted did resolve one question I had about the mapView.annotations array and it's ordering. When I was testing my application using my code where I was pulling data from the mapView.annotations array it was scrambled. I thought it had to do with network latency as it was fed data from a JSON stream (as my code indicates). I understand now that Apple doesn't order that data in the same fashion as it went in which is where my confusion stemmed from. As you can see I am doing a few other conversions of the original data being fed into my Annotations and that is working for me now. – user3138529 Dec 28 '13 at 18:00

0 Answers0