[{"name":"Mocha","latitude":17.418694 , "longitude":78.445116},
{"name":"Rock Castle","latitude":17.420865 , "longitude":78.442219},
{"name":"RnB Select","latitude":17.420639 , "longitude":78.443635}
]
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
location=locations.lastObject;
[[self latitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.latitude]];
[[self longnitudeValue] setText:[NSString stringWithFormat:@"%.6f",location.coordinate.longitude]];
// [[self altitudeValue] setText:[NSString stringWithFormat:@"%.2f feet",location.altitude*METERS_FEET]];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 2*METERS_MILE, 2*METERS_MILE);
[[self mapView] setRegion:viewRegion animated:YES];
NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"myStoredObject"];
NSDictionary *zeroth=arr[0];
NSLog(@"oth position,,,,%@",zeroth);
NSDictionary *firstPosition = arr[1];
NSLog(@"first position is .....%@",firstPosition);
NSDictionary *secondPosition = arr[2];
NSLog(@"second position is .....%@",secondPosition);
MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate =CLLocationCoordinate2DMake([[firstPosition valueForKey:@"latitude"] doubleValue], [[firstPosition valueForKey:@"longitude"] doubleValue]);
point2.title =[firstPosition valueForKey:@"name"];
[self.mapView addAnnotation:point2];
MKPointAnnotation *point3 = [[MKPointAnnotation alloc] init];
point3.coordinate = CLLocationCoordinate2DMake([[firstPosition valueForKey:@"latitude"] doubleValue], [[firstPosition valueForKey:@"longitude"] doubleValue]);
point3.title =[firstPosition valueForKey:@"name"];
[self.mapView addAnnotation:point3];
MKPointAnnotation *point4 = [[MKPointAnnotation alloc] init];
point4.coordinate = CLLocationCoordinate2DMake([[secondPosition valueForKey:@"latitude"] doubleValue], [[secondPosition valueForKey:@"longitude"] doubleValue]);
point4.title =[secondPosition valueForKey:@"name"];
[self.mapView addAnnotation:point4];
MKCircle *circleoverlay = [MKCircle circleWithCenterCoordinate:_mapView.userLocation.coordinate radius:1000];
[circleoverlay setTitle:@"Circle"];
[_mapView addOverlay:circleoverlay];
NSLog(@"circle is drawn");
}
code ci-dessus montrant les données JSON ... Ce mes données JSON qui Iget d'URL en écrivant le code ci-dessousComment ajouter les données JSON à NSARRAY?
NSMutableURLRequest *tRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.ctrlm.in/kvikdata/locations.json"]];
NSLog(@"url is %@",tRequest);
[tRequest setHTTPMethod:@"GET"];
[tRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:tRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error == nil){
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error;
id jsonObject = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSLog(@"Successfully deserialized.2222222..%@",jsonObject);
NSArray * myArr = [copie JSONObject]; // ici myArr = yourData de votre réponse. je prends nil pour démontrer NSLog (@ "mon tableau est% @", myArr);
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:myArr forKey:@"myStoredObject"]; // by this you can store object
if (jsonObject != nil && error == nil){
NSLog(@"Successfully deserialized...%@",jsonObject);
}
});
}
}];
en écrivant les NSUserDefaults premières données est mise à jour selon l'objet JSON .. à nouveau quand je change les noms des lieux de @ « mocha » à @ « test1 » est-ce pas mis à jour ... pourquoi? Merci u
Les données json que vous avez publiées ressemblent à un tableau. –
Pouvez-vous s'il vous plaît indent correctement et généralement nettoyer votre code? C'est plus ou moins illisible. Soyez également cohérent, où vous mettez des espaces et où pas! –