gpt4 book ai didi

iphone - 从 URL 读取 JSON 并添加 MKAnnotations

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:50 25 4
gpt4 key购买 nike

我已经阅读了几个不同的教程,试图让它工作,但它们似乎掩盖了初学者可能不知道的一些关键步骤。

我在一个 URL 上有一个 JSON 文件,其中列出了名称、纬度和经度。我如何将它导入数组或字典(我不知道它们的区别),然后迭代它并在每次迭代时创建一个新的注释。

IOS6, Storyboard

_ 添加代码 _

ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>


@interface ViewController : UIViewController {}

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic, strong) NSMutableData *downloadData;

@end

ViewController.m

#import "ViewController.h"
#import "MapViewAnnotation.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

_downloadData = [NSMutableData new];

NSURL *requestURL = [NSURL URLWithString:@"OMITTED/apptest/locations.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_downloadData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
id parsed = [NSJSONSerialization JSONObjectWithData:_downloadData options:kNilOptions error:nil];
for (NSDictionary *pointInfo in parsed)
{
NSLog([parsed objectForKey:@"name"]);
double xCoord = [(NSNumber*)[parsed objectForKey:@"lat"] doubleValue];
double yCoord = [(NSNumber*)[parsed objectForKey:@"lon"] doubleValue];
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(xCoord, yCoord);


MKPointAnnotation *point = [MKPointAnnotation new];
point.coordinate = coords;
point.title = [parsed objectForKey:@"name"];

[self.mapView addAnnotation:point]; // or whatever your map view's variable name is
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)viewDidUnload {
[super viewDidUnload];
// ARC Problem --- [_mapView release];
self.mapView = nil;
}

@end

最佳答案

在 iOS 5 中,有一个 JSONSerializer 类可以根据需要将原始 JSON 数据从您的 URL 转换为数组或字典。

您需要从服务器下载数据:

NSURL *requestURL = [NSURL URLWithString:@"<your url here>"];
NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];

然后您将添加这些委托(delegate)方法:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_downloadData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
id parsed = [NSJSONSerialization JSONObjectWithData:_downloadData options:kNilOptions error:nil];
}

_downloadData 是您的 NSMutableData 类的实例变量或属性。

parsed 变量将包含来自服务器的数据。如果它是一个点列表,它可能是一个数组,因此您可以使用快速枚举遍历它:

for (NSDictionary *pointInfo in parsed) {
double xCoord = [(NSNumber*)[parsed objectForKey:@"<key for lat coord>"] doubleValue];
double yCoord = [(NSNumber*)[parsed objectForKey:@"<key for long coord>"] doubleValue];
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(xCoord, yCoord);


MKPointAnnotation *point = [[MKPointAnnotation new] autorelease];
point.coordinate = coords;
point.title = [parsed objectForKey:@"<key for title>"];

[self.mapView addAnnotation:point]; // or whatever your map view's variable name is
}

关于iphone - 从 URL 读取 JSON 并添加 MKAnnotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14864825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com