我正在使用Google map iOS sdk来获取方向。通过这个LINK帮我绘制两点之间的路线。现在我不能给最终用户提供路线指示(例如:向左转,向右转) 。如何解决这个问题?请帮帮我。现在我正在使用以下代码
//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 :(得分: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 }
}