0
我記得有一個Google API可以從經緯度或&尋址,如果我們在webview上加載該網址,它會用來顯示路線以Text形式的兩點之間。任何人都可以提供API來獲取基於文本的路線。我不想在地圖上畫線,想要顯示文字。如何在iOS中以文本的形式獲取兩個位置之間的路線
我記得有一個Google API可以從經緯度或&尋址,如果我們在webview上加載該網址,它會用來顯示路線以Text形式的兩點之間。任何人都可以提供API來獲取基於文本的路線。我不想在地圖上畫線,想要顯示文字。如何在iOS中以文本的形式獲取兩個位置之間的路線
MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.776142, -122.424774) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
[srcMapItem setName:@""];
MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.73787, -122.373962) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];
MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
[distMapItem setName:@""];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
[request setSource:srcMapItem];
[request setDestination:distMapItem];
[request setTransportType:MKDirectionsTransportTypeAny];
MKDirections *direction = [[MKDirections alloc]initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
NSLog(@"response = %@",response);
NSArray *arrRoutes = [response routes];
[arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKRoute *rout = obj;
MKPolyline *line = [rout polyline];
[mapview addOverlay:line];
NSLog(@"Rout Name : %@",rout.name);
NSLog(@"Total Distance (in Meters) :%f",rout.distance);
NSArray *steps = [rout steps];
NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);
NSMutableArray *stepsArray=[[NSMutableArray alloc] init];
[steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Rout Instruction : %@",[obj instructions]);
NSLog(@"Rout Distance : %f",[obj distance]);
[stepsArray addObject:[obj instructions]];
}];
[self myShowDirections:response];
self.steps.text=[NSString stringWithFormat:@"%@",stepsArray];
你可以使用它並得到你想要的。