【发布时间】:2016-04-13 12:56:41
【问题描述】:
[{"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");
}
以上代码显示了 Json 数据... 这是我通过编写以下代码从 URL 获取的 JSON 数据
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 = [jsonObject 副本]; // 这里 myArr = yourData 来自您的响应。我拿它 nil 来演示 NSLog(@"我的数组是 %@",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);
}
});
}
}];
通过写入 NSUserDefaults,第一个数据会根据 json 对象进行更新。再次,当我将位置名称从 @"mocha" 更改为 @"test1" 时,它没有更新...为什么? 谢谢你
【问题讨论】:
-
你发布的json数据看起来像一个数组。
-
您能否正确缩进并清理您的代码?它或多或少不可读。还要保持一致,放置空格的位置和不放置空格的位置!
标签: objective-c json nsurlrequest