MapKit / CoreLocation iOS7为什么我不能将注释数据(坐标)保存到数组?

时间:2013-12-27 06:08:52

标签: ios7 annotations mapkit xcode5 core-location

好吧,我看起来很高,也没有找到答案。为什么我可以在mapview.annotations数组中查询坐标数据,但是我不能将生成的相同注释数据保存到一个可以相同方式查询的数组中。

这是我的代码:

- (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]);
}


}

和处理我的点之间的比较的代码,我试图从我的数组中提取数据。

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


}

}

如果我objectIntIndex self.mapView.annotations我得到正确的lat和lon,所以我猜它是一个无效的类型转换,因为它进入数组我是否正确的想法?

我能做得更好吗?我只是在使用数组,因为mapView.annotations根据网络速度是不可靠的,注释视图在哪个索引处有很多代码。

目标是比较loc1和loc2获取它们之间的距离。将当前位置与loc1的距离进行比较,将当前位置与loc2的距离相加,如果在误差范围内,则检查它是否小于或等于。但我无法做到这一点,直到我找到一种可靠的方法来了解哪个mapView.annotation是哪个。

也许我是瞎子。谢谢!

解答:

再次查看我的代码,决定使用我之前将我的JSON输入的数组,然后从那里提取数据,因为这将允许它在未来进一步扩展。这是我结束的代码,似乎工作正常。

 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;

    }



}


}

0 个答案:

没有答案