github から MKMapView.check nvpolylineプロジェクトを使用すると、これははるかに簡単になります。点aと点bの間に線を作成するのに役立ち、最初の問題を解決します.
2番目の問題については、このコードをプロジェクトに追加してください
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];
//&alternatives=true
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:apiResponse error:nil];
NSLog(@"apiResponse is : %@",jsonData);
//to display route in drop down
NSArray *routeArray = [[NSArray alloc]init];
routeArray= [jsonData objectForKey:@"routes"];
for(int i=0;i<[routeArray count];i++)
{
NSDictionary *tempDictionary = [routeArray objectAtIndex:i];
if([tempDictionary objectForKey:@"overview_polyline"]!=nil)
{
NSDictionary *secTempDictionary = [tempDictionary objectForKey:@"overview_polyline"];
if([secTempDictionary objectForKey:@"points"]!=nil)
{
NSString * routePoint =[secTempDictionary objectForKey:@"points"];
[routeSetAry addObject:routePoint];
encodedPoints = [secTempDictionary objectForKey:@"points"];
}
// NSLog(@"secTempDictionary is: %@", secTempDictionary);
}
if([tempDictionary objectForKey:@"legs"]!=nil)
{
NSArray *lagArray = [[NSArray alloc]init];
lagArray= [tempDictionary objectForKey:@"legs"];
for(int i=0;i<[lagArray count];i++)
{
NSDictionary *thirdTempDictionary = [lagArray objectAtIndex:i];
if([thirdTempDictionary objectForKey:@"steps"]!=nil)
{
NSArray *stepsArray = [[NSArray alloc]init];
stepsArray= [thirdTempDictionary objectForKey:@"steps"];
for(int i=0;i<[stepsArray count];i++)
{
NSDictionary *forthTempDictionary = [stepsArray objectAtIndex:i];
if([forthTempDictionary objectForKey:@"html_instructions"]!=nil)
{
NSString * directionStr =[forthTempDictionary objectForKey:@"html_instructions"];
NSRange range;
while ((range = [directionStr rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
directionStr=[directionStr stringByReplacingCharactersInRange:range withString:@""];
}
[directionStrAry addObject:directionStr];
}
NSDictionary *fifthTempDictionary = [forthTempDictionary objectForKey:@"polyline"];
if([fifthTempDictionary objectForKey:@"points"]!=nil)
{
NSString * routePoint =[fifthTempDictionary objectForKey:@"points"];
[polylineSetAry addObject:routePoint];
// encodedPoints = [fifthTempDictionary objectForKey:@"points"];
}
NSDictionary *sixthTempDictionary =[forthTempDictionary objectForKey:@"distance"];
if([sixthTempDictionary objectForKey:@"text"]!=nil)
{
NSString * distanceStr =[sixthTempDictionary objectForKey:@"text"];
[distanceStrAry addObject:distanceStr];
// encodedPoints = [fifthTempDictionary objectForKey:@"points"];
}
}
}
}
}
}
NSLog(@"routeSetAry is :%@",routeSetAry);
NSLog(@"polylineSetAry is : %i",polylineSetAry.count);
これにより、方向文字列の配列が得られ、この文字列を使用してプロジェクトのどこにでも表示できます。
私のプロジェクトでは、マップの上部に表示される小さなビューを作成しました。そのビューでは、方向を 1 つずつ表示しています。