1

在我的項目中,我在同一個課堂中使用了2個API。我想獲得數據使用NSURLRequest,在第一個API我得到的地方的緯度經度,並把這些經度的第二個網址獲得這些地方之間的距離,但我得到了與每個地方相同的距離當使用多個網址時,使用NSURLRequest獲取錯誤數據

這裏是我的代碼。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ 
    currentLocation = newLocation; 

    if (currentLocation != nil){ 

     NSString *str=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=2000&types=%@&sensor=false&key=API Key",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,strCatText]; 
     [self createTheConnection:str]; 
     //[self createTheConnection1:str1]; 

     arrAllData=[[NSMutableArray alloc]init]; 
     int i; 
     for (i=0; i<=2; i++) 
     { 
      NSString *strname=[[[arr valueForKey:@"results"] objectAtIndex:i] valueForKey:@"name"]; 
      NSString *strvicinity=[[[arr valueForKey:@"results"] objectAtIndex:i] valueForKey:@"vicinity"]; 
      NSString *strRef=[[[arr valueForKey:@"results"]objectAtIndex:i]valueForKey:@"reference"]; 
      NSString *strLat=[[[[[arr valueForKey:@"results"] objectAtIndex:i] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"]; 
      NSString *strLng=[[[[[arr valueForKey:@"results"] objectAtIndex:i] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"]; 

      int j; 
      for (j=0; j<=0; j++) 
      { 
       NSString *str1=[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%@,%@&sensor=false&mode=driving",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,strLat,strLng]; 
       //NSLog(@"%@",str1); 
       [self createTheConnection1:str1]; 

       // if (![strLat isEqual:@"0"]) { 
       // [email protected]""; 
        NSString *strDistance=[[[[[[arr2 valueForKey:@"routes"] objectAtIndex:0] valueForKey:@"legs"] objectAtIndex:0] valueForKey:@"distance"] valueForKey:@"text"]; 
       // NSLog(@"%@",strDistance); 
       //} 

       if(strname == nil){ 
        strname = [NSString stringWithFormat:@"%@", [NSNull null]]; 
       } 
       if(strvicinity == nil){ 
        strvicinity = [NSString stringWithFormat:@"%@", [NSNull null]]; 
       } 
       if(strRef == nil){ 
        strRef = [NSString stringWithFormat:@"%@", [NSNull null]]; 
       } 
       if(strDistance == nil){ 
        strDistance = [NSString stringWithFormat:@"%@", [NSNull null]]; 
       } 

       if (![strDistance isEqual:@"<null>"]) { 
        arrdata=[[NSMutableArray alloc]initWithObjects:strname,strvicinity,strRef,strDistance, nil]; 
        strDistance=nil; 

        [arrAllData addObject:arrdata]; 
       }  

      }   
      [tblView reloadData]; 
     } 
    } 



-(void)createTheConnection:(NSString*)strUrl 
{  
    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 

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

    if(connections) 
    { 
     webData=[NSMutableData data]; 

    } 

} 

-(void)createTheConnection1:(NSString*)strUrl1 
{ 

    NSMutableURLRequest *request1=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[strUrl1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 

    connections1=[[NSURLConnection alloc]initWithRequest:request1 delegate:self]; 

    if(connections1) 
    { 
     webData1=[NSMutableData data]; 

    } 

} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 

    // //NSLog(@"%@",error); 

    self.navigationItem.leftBarButtonItem=nil; 

} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    if (connection==connections) { 
     [webData appendData:data]; 
    } 
    if (connection==connections1) { 
     [webData1 appendData:data]; 
    } 


} 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    if (connection==connections) { 
     [webData setLength:0]; 
    } 
    if (connection==connections1) { 
     [webData1 setLength:0]; 
    } 


} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 

    NSError *err; 
    if (connection==connections) { 
     arr=[NSJSONSerialization JSONObjectWithData:webData options:NSJSONReadingMutableContainers error:&err]; 
    } 
    if (connection==connections1) { 
     arr2=[NSJSONSerialization JSONObjectWithData:webData1 options:NSJSONReadingMutableContainers error:&err];  
    } 
} 


-(void)barBtnItem1{ 
    MapViewController *mvc =[self.storyboard instantiateViewControllerWithIdentifier:@"mapview"]; 
    [UIView beginAnimations:@"Start" context:nil]; 
    [UIView setAnimationDuration:0.80]; 
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO]; 
    [self.navigationController pushViewController:mvc animated:YES]; 
    [UIView commitAnimations]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return arrAllData.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    UILabel *lblName=(UILabel*)[cell viewWithTag:100]; 
    UILabel *lblAddress=(UILabel*)[cell viewWithTag:101]; 
    UILabel *lblDistance=(UILabel*)[cell viewWithTag:102]; 
    arrData11=[arrAllData objectAtIndex:indexPath.row]; 
    NSString *strName=[NSString stringWithFormat:@"%@",arrData11[0]]; 
    NSString *strAddress=[NSString stringWithFormat:@"%@",arrData11[1]]; 
    NSString *strDistance1=[NSString stringWithFormat:@"%@",arrData11[3]]; 
    if ([strName isEqual:@"<null>"]) { 
//  [email protected]"Loading..."; 
//  [email protected]"Loading..."; 
    } 
    else{ 
     lblName.text=strName; 
     lblAddress.text=strAddress; 
     lblDistance.text=strDistance1; 
    } 
     return cell; 
} 
+0

所有代碼都可以@suhit – sanjeet

回答

0

你正在創建兩個連接背靠背,當他們收到他們呼籲與會代表的數據,但在你應用程式你沒有迫不及待地想從第一次請求的數據,然後進行第二次請求。我很確定你得到的距離是第二次經緯度的距離。

對這些數據發出單一請求並查看您獲得的距離。並嘗試逐個提出請求。即首先請求..獲取數據..然後提出第二個請求。因爲他們使用相同的委託來初始化數據。

編輯 我在移動,所以我不能幫你代碼。我來試試..

Method A (take parameter that you need to make a url request) { 
    //Make request with pair of lat and long and necessary datas 
} 

(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
    ........ 
    //After getting the data 

    Call method A with next pair of lat long 
    You can determine next pair by maintaining a global variable. 
} 

讓我知道它幫助... :)

+0

我怎麼能解決這個問題,我很新與GCD ..告訴我PLZ – sanjeet

+0

@sanjeet看我的編輯。 – Rashad

+0

@sanjeet >>你解決了這個問題嗎? – Rashad

相關問題