ios - 用于在 mapView 上显示标记的 JSON 连接

标签 ios nsurlconnection nsurlrequest nsjsonserialization

我正在尝试执行三个不同的 JSON 连接,以使用来自三个不同 MySQL 表的标记填充 mapView。

这是我现在要做的代码。为了测试连接,我记录了 didReceiveData 方法,但编译器光标没有到达该方法,当然,任何标记都显示在 map 上。

恳请您查看我的代码并告诉我我遗漏了什么或做错了什么。谢谢。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Geolocalización";


    //carga de datos JSON
    //network activity indicator showed during downloading data
    //PROCESSING FIRST CONNECTION
    first_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];
    NSURLRequest *first_connection_request = [NSURLRequest requestWithURL:first_connection_url];
    NSURLConnection *first_connection=[[NSURLConnection alloc]initWithRequest:first_connection_request delegate:self];

    //PROCESSING SECOND CONNECTION
    second_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/centrostodoslist.php"];
    NSURLRequest *second_connection_request = [NSURLRequest requestWithURL:second_connection_url];
    NSURLConnection *second_connection=[[NSURLConnection alloc]initWithRequest:second_connection_request delegate:self];
    //PROCESSING THIRD CONNECTION
    third_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/eventostodoslist.php"];
    NSURLRequest *thrid_connection_request = [NSURLRequest requestWithURL:third_connection_url];
    NSURLConnection *third_connection=[[NSURLConnection alloc]initWithRequest:second_connection_request delegate:self];


}

//methods to perform the connection and population of data

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if(connection==first_connection){
        data_for_first_connection = [[NSMutableData alloc]init];

    }
    else if(connection==second_connection){
        data_for_second_connection = [[NSMutableData alloc]init];
    }
    else if(connection==third_connection){
        data_for_third_connection = [[NSMutableData alloc]init];
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
    if(connection==first_connection){
        [data_for_first_connection appendData:thedata];
        NSLog(@"CONEXION 1 AQUI");
    }
    else if(connection==second_connection){
        [data_for_second_connection appendData:thedata];
     NSLog(@"CONEXION 2 AQUI");
    }
    else if(connection==third_connection){
        [data_for_third_connection appendData:thedata];
     NSLog(@"CONEXION 3 AQUI");
    }
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //if data received network indicator not visible
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    if(connection==first_connection) {

        categorias_first = [NSJSONSerialization JSONObjectWithData:data_for_first_connection options:0 error:nil];

    }
    else if(connection==second_connection){

        categorias_second = [NSJSONSerialization JSONObjectWithData:data_for_second_connection options:0 error:nil];
    }
    else if(connection==third_connection){
        // PROCESS TO BE DONE FOR SECOND CONNECTION
        categorias_third = [NSJSONSerialization JSONObjectWithData:data_for_third_connection options:0 error:nil];
    }

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:27.9847
                                                            longitude:-15.5953
                                                                 zoom:9];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView_.myLocationEnabled = YES;
     mapView_.delegate = self;
    self.view = mapView_;

    UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Ofertas", @"Cursos", @"Eventos", nil]];
    [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    mainSegment.frame = CGRectMake(20,80, 280, 43);
    //self.navigationItem.titleView = mainSegment;
    //mainSegment.selectedSegmentIndex = nil;
    [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged];
    [self.view addSubview:mainSegment];


}

用于测试应用程序的更新代码。

我已经更新了我的代码来测试它,现在我只使用两个连接。在记录结果时,我检测到数组 categoria(在 first_connection)和 categoria2(在 second_connection)是从同一个 JSON 文件(从 first_connection)填充的:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Geolocalización";



    //carga de datos JSON
    //network activity indicator showed during downloading data
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

    //URL definition where php file is hosted
    //conexion1
    url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];

    request = [NSURLRequest requestWithURL:url];

    first_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];


    //conexion2
    url2 = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/centrostodoslist.php"];

    request2 = [NSURLRequest requestWithURL:url];

    second_connection = [[NSURLConnection alloc] initWithRequest:request2 delegate:self];






}

//methods to perform the connection and population of data

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    if ([connection isEqual:first_connection]){
        NSLog(@"FIRST CONNECTION RESPONSE");
        data = [[NSMutableData alloc]init];
    }

    if ([connection isEqual:second_connection]){
        NSLog(@"SECOND CONNECTION RESPONSE");
        data2 = [[NSMutableData alloc]init];
    }


    NSLog(@"ESTOY EN DIDRECEIVERESPONSE");
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{


    if ([connection isEqual:first_connection]){
        NSLog(@"FIRST CONNECTION RECEIVED");
       [data appendData:thedata];

    }
    if ([connection isEqual:second_connection]){
        NSLog(@"SECOND CONNECTION RECEIVED");
        [data2 appendData:thedata];

    }




}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //if data received network indicator not visible
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    //array waterfalls populated via JSON from database

    if ([connection isEqual:first_connection]){
    categorias = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"DATA1 = %@",categorias);
    }
    if ([connection isEqual:second_connection]){
    categorias2 = [NSJSONSerialization JSONObjectWithData:data2 options:0 error:nil];
        NSLog(@"DATA2 = %@",categorias2);
    }
    NSLog(@"NUMERO DE EMPRESAS OPCION1= %lu"
          , (unsigned long)[categorias count]);
    NSLog(@"NUMERO DE EMPRESAS OPCION2= %lu"
          , (unsigned long)[categorias2 count]);
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:27.9847
                                                            longitude:-15.5953
                                                                 zoom:9];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView_.myLocationEnabled = YES;
     mapView_.delegate = self;
    self.view = mapView_;




    UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Ofertas", @"Cursos", @"Eventos", nil]];
    [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    mainSegment.frame = CGRectMake(20,80, 280, 43);
    //self.navigationItem.titleView = mainSegment;
    //mainSegment.selectedSegmentIndex = nil;
    [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged];
    [self.view addSubview:mainSegment];


}

最佳答案

我看到了几个问题。首先,您的连接、firstConnection、secondConnection 等被创建为局部变量,因此一旦 viewDidLoad 超出范围,它们就会被释放。当您检查委托(delegate)方法中的相等性时,它们此时将为 nil。您应该为这些连接创建 ivars 或属性。此外,您不应使用“==”来比较对象,而应使用 isEqual:。

else if([connection isEqual:third_connection]){

关于ios - 用于在 mapView 上显示标记的 JSON 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22342458/

相关文章:

ios - 如何在用户获取当前位置时刷新或移动图钉

iphone - 表头和表格单元格之间的空间

iphone - 如何使用 NSURLRequest/NSURLConnection 将 mp3 文件下载到应用程序?

objective-c - NSURLSession 委托(delegate) : How to implement my custom SessionDelegate class accordingly?

iOS 推特 NSURLErrorDomain Code=-1012

ios - 在 ios 的 UIWebview 中调用 javaScript

ios - 在 Xcode 10 中找不到架构 x86_64 的符号(错误)

ios - 如何解决由坏的 HTTP 持久连接引起的超时问题?

swift - Swift 中的 NSURLConnection : How to call a function when it's finished loading, 并在 URL 为 404 时重新启动它?

ios - 确定从 iPhone 中的 URL 检索 JSON 的问题