3 개의 다른 JSON 연결을 수행하여 mapView에 마커가있는 세 개의 다른 MySQL 테이블을 채우려 고합니다.mapView에 마커를 표시하는 JSON 연결
이것은 지금 제가 수행 한 코드입니다. 연결을 테스트하기 위해 didReceiveData 메서드를 로깅하지만 컴파일러 커서가 해당 메서드에 도착하지 않으며 물론 모든 마커가지도에 표시됩니다.
내 코드를보고 내가 무엇을 놓치고 잘못했는지 알려주는 것이 좋습니다. 고맙습니다.
- (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];
}
APP를 테스트하기위한 업데이트 된 코드.
테스트를 위해 코드를 업데이트 했으므로 이제는 두 개의 연결로만 작업하고 있습니다. 결과를 기록, 나는 배열 (first_connection에서) 제품 분류 및 (second_connection에서) categoria2이 (first_connection에서) 같은 JSON 파일에서 설치되었는지 감지 :
- (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];
}
'initWithRequest'대신 'connectionWithRequest'를 사용할 수 있습니까? – user2071152
@ user2071152 고맙습니다. 지금 시도해 보겠습니다.하지만 문제가 아닌 것 같습니다. 처음에는 연결을 하나만 시도했지만 제대로 작동했습니다. – mvasco
@ user2071152 귀하의 제안은 컴파일러 오류를 발생시킵니다. – mvasco