2013-11-22 78 views
0

我正在使用​​獲取方向。通過此LINK幫助我繪製兩點之間的路線。現在我無法給出路線指令(例如:向左轉,轉向正確)給最終用戶。如何解決這個問題請幫助me.Now我使用下面的代碼如何知道谷歌地圖上兩個位置之間的每個路線位置iOS sdk

//Request the url 
-(void)getWayPoints:(CLLocationCoordinate2D)origin destinationIS:(CLLocationCoordinate2D)desti{ 

NSString *oLat = [NSString stringWithFormat:@"%f",origin.latitude]; 
NSString *oLong = [NSString stringWithFormat:@"%f",origin.longitude]; 

NSString *dLat = [NSString stringWithFormat:@"%f",desti.latitude]; 
NSString *dLong = [NSString stringWithFormat:@"%f",desti.longitude]; 

NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?&origin=%@,%@&destination=%@,%@&sensor=false",oLat,oLong,dLat,dLong]; 
NSLog(@"url : %@", url); 
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
                  origin.latitude, 
                  origin.longitude); 
GMSMarker *marker = [GMSMarker markerWithPosition:position]; 
marker.map = mapView_; 

CLLocationCoordinate2D position1 = CLLocationCoordinate2DMake(
                  desti.latitude, 
                  desti.longitude); 
GMSMarker *marker1 = [GMSMarker markerWithPosition:position1]; 
marker1.map = mapView_; 

NSURL *googleRequestURL=[NSURL URLWithString:url]; 
NSLog(@"googleRequestURL : %@", googleRequestURL); 

dispatch_async(kBgQueue, ^{ 
    NSData* data = [NSData dataWithContentsOfURL: googleRequestURL]; 
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; 
}); 



//Response from URL 

- (void)fetchedData:(NSData *)responseData { 
//parse out the json data 
NSError* error; 
// NSLog(@"responseData Data: %@", responseData); 
NSDictionary* json = [NSJSONSerialization 
         JSONObjectWithData:responseData 

         options:kNilOptions 
         error:&error]; 
NSArray* places = [json objectForKey:@"routes"]; 

NSDictionary *routes = [json objectForKey:@"routes"][0]; 

NSDictionary *route = [routes objectForKey:@"overview_polyline"]; 

NSArray *routes1 = [json objectForKey:@"routes"]; 
NSArray *legs = [routes1[0] objectForKey:@"legs"]; 
NSLog(@"legs %@", legs); 
NSArray *steps = [legs[0] objectForKey:@"steps"]; 

NSString *overview_route = [route objectForKey:@"points"]; 
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route]; 
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; 
polyline.strokeColor = [UIColor redColor]; 
polyline.strokeWidth = 5.f; 
polyline.map = mapView_; 

}

回答

0
NSString *str=[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false",self.txtFrom.text,self.txtTo.text]; 

NSURL *url=[[NSURL alloc]initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 


NSError* error; 

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSArray* latestLoans = [json objectForKey:@"routes"]; 

NSMutableDictionary *legs=[[[latestLoans objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0]; 

NSString *startLocation,*endLocation,*totalDistance,*totalDuration; 

CLLocationCoordinate2D startLoc,endLoc; 

startLocation = [legs objectForKey:@"start_address"]; 

endLocation = [legs objectForKey:@"end_address"]; 

totalDistance = [[legs objectForKey:@"distance"] objectForKey:@"text"]; 

totalDuration = [[legs objectForKey:@"duration"] objectForKey:@"text"]; 

startLoc=CLLocationCoordinate2DMake([[[legs objectForKey:@"start_location"] objectForKey:@"lat"] doubleValue], [[[legs objectForKey:@"start_location"] objectForKey:@"lng"] doubleValue]); 

endLoc=CLLocationCoordinate2DMake([[[legs objectForKey:@"end_location"] objectForKey:@"lat"] doubleValue], [[[legs objectForKey:@"end_location"] objectForKey:@"lng"] doubleValue]); 


NSArray *steps=[legs objectForKey:@"steps"]; 

NSMutableDictionary *tempDict; 

if ([steps count]!=0) { 


    MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * [steps count]+2); 

    [tv setText:@""]; 

    for(int idx = 0; idx < [steps count]+2; idx++) 
    { 

     CLLocationCoordinate2D workingCoordinate; 

     if (idx==0) { 

      workingCoordinate=startLoc; 

      MKMapPoint point = MKMapPointForCoordinate(workingCoordinate); 

      pointArr[idx] = point; 

      [tv setText:[NSString stringWithFormat:@"%@ %@",tv.text,@"START"]]; 

     } 

     else if (idx==[steps count]+1){ 

      workingCoordinate=endLoc; 

      MKMapPoint point = MKMapPointForCoordinate(workingCoordinate); 

      pointArr[idx+1] = point; 

      [tv setText:[NSString stringWithFormat:@"%@ %@",tv.text,@"\nEND"]]; 

     } 

     else{ 

      MKPolyline *pol= [self polylineWithEncodedString:[[[steps objectAtIndex:idx-1] objectForKey:@"polyline"] objectForKey:@"points"]]; 

      MKMapPoint point = *(pol.points); 

      pointArr[idx] = point; 

      [tv setText:[NSString stringWithFormat:@"%@\n\n%@",tv.text,[self flattenHTML:[[steps objectAtIndex:idx-1] objectForKey:@"html_instructions"]]]]; 

     } 


     tempDict = nil; 
    } 

    // create the polyline based on the array of points. 
    [mapView removeOverlay:routeLine]; 

    routeLine = [MKPolyline polylineWithPoints:pointArr count:[steps count]]; 

    [mapView addOverlay:routeLine]; 

    free(pointArr); 
} 

與以下支持的方法:?

-(NSArray*) calculateRoutesFrom:(NSString *) f to: (NSString ) t { NSString apiUrlStr = [NSString stringWithFormat:@" http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@ ", f, t];

NSURL* apiUrl = [NSURL URLWithString:apiUrlStr]; NSLog(@"api url: %@", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:nil]; @try {

// TODO: better parsing. Regular expression? 

    NSInteger a = [apiResponse indexOf:@"points:\"" from:0]; 
    NSInteger b = [apiResponse indexOf:@"\",levels:\"" from:a] - 10; 

    NSInteger c = [apiResponse indexOf:@"tooltipHtml:\"" from:0]; 
    NSInteger d = [apiResponse indexOf:@"(" from:c]; 
    NSInteger e = [apiResponse indexOf:@")\"" from:d] - 2; 

    NSString* info = [[apiResponse substringFrom:d to:e] stringByReplacingOccurrencesOfString:@"\\x26#160;" withString:@""]; 
    NSLog(@"tooltip %@", info); 

    NSString* encodedPoints = [apiResponse substringFrom:a to:b]; 

    return [self decodePolyLine:[encodedPoints mutableCopy]]; 

} 
@catch (NSException * e) { 
    // TODO: show error 
}  

}

相關問題