在我的項目中,我在同一個課堂中使用了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;
}
所有代碼都可以@suhit – sanjeet