3

MapKit api を使用して、マップ上の現在の位置を取得し、ドロップ ピンが指す 2 つの場所の間のルートを描画しています。また、ルート間のすべてのメイン スタンドを取得したいと考えています。以下の関数を使用して、2 つの場所間のルートを取得しています

- (NSArray*)getRoutePointFrom:(MyLocation*)origin to:(MyLocation*)destination
{
 NSString* saddr = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
 NSString* daddr = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];


 NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false&avoid=highways&mode=driving",saddr,daddr]];

 NSError *error=nil;

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

 [request setURL:url];
 [request setHTTPMethod:@"POST"];

  NSURLResponse *response = nil;

  NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: &error];

  NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

 SBJsonParser *json=[[SBJsonParser alloc] init];

 NSDictionary *dic=[json objectWithString:responseString error:nil];

 NSDictionary *nextdic=[dic valueForKey:@"routes"];
 NSDictionary *legdic=[nextdic valueForKey:@"legs"];
 NSDictionary *stepdic=[legdic valueForKey:@"steps"];

 NSArray *array=[[NSArray alloc] initWithArray:[[stepdic valueForKey:@"polyline"] valueForKey:@"points"]];  


 NSString *string=[NSString stringWithFormat:@"%@",[[array objectAtIndex:0] objectAtIndex:0]];




 return [self decodePolyLine:[string mutableCopy]];

}



-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr 
{  

 NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];  
 [encoded appendString:encodedStr];  
 [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"  
                             options:NSLiteralSearch  
                               range:NSMakeRange(0, [encoded length])];  
 NSInteger len = [encoded length];  

 NSInteger index = 0;  
 NSMutableArray *array = [[NSMutableArray alloc] init] ;  
 NSInteger lat=0;  
 NSInteger lng=0;  
 while (index < len) {  
  NSInteger b;  
  NSInteger shift = 0;  
  NSInteger result = 0;  
  do {  
   b = [encoded characterAtIndex:index++] - 63;  
   result |= (b & 0x1f) << shift;  
   shift += 5;  
  } while (b >= 0x20);  
  NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));  
  lat += dlat;  
  shift = 0;  
  result = 0;  
  do {  
   b = [encoded characterAtIndex:index++] - 63;  
   result |= (b & 0x1f) << shift;  
   shift += 5;  
  } while (b >= 0x20);  
  NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));  
  lng += dlng;  
  NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];  
  NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];  
  //          printf("[%f,", [latitude doubleValue]);  
  //          printf("%f]", [longitude doubleValue]);  
  CLLocation *loc =[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;  
  [array addObject:loc];  
 }  

 NSLog(@"array in decode polygon is %@",array);

 return array;  
}

しかし、それは機能していません。..

これについて助けてくださいありがとう!...

4

2 に答える 2

5

質問は何度かされています。http://iosguy.com/2012/05/22/tracing-routes-with-mapkit/からコードを取得していると思います。 また、その SO の質問を見ることもできます: iOS で複数のポイントを持つルートをプロットする

http://iosboilerplate.comからコードを取得して、貢献することもできます。

そして最後になりましたが、少額のお金でそれを行うのに役立つフレームワークがあります (ただし、同じことを行うのにかかるものと比較して何もありません): http://www.cocoacontrols.com/controls/ mtdirectionskit

于 2012-09-19T09:19:20.257 に答える