2016-06-27 56 views
-2

我正在尝试两个location.it之间的地图上的绘制路径线,它不是在道路上显示它就像道路上两个地方标记之间的最短距离。iPhone上的地图上的路径线绘制

我已经尝试过在路上对齐。 我MKDirectionsRequest以及(这是给错误,我搜索,然后显示这是不是在印度工作的一些地方。)

任何其他方式来绘制道路线。

+0

你能告诉我你做了什么?我的意思是代码? –

+0

你使用的是谷歌地图吗? –

+0

http://stackoverflow.com/questions/10598322/iphone-how-to-draw-line-between-two-points-on-mapkit –

回答

0

您可以使用googleMAP API进行此操作。

- (void)drawRoute :(CLLocationCoordinate2D)myOrigin destination:(CLLocationCoordinate2D)myDestination 
{ 
     [self fetchPolylineWithOrigin:myOrigin destination:myDestination completionHandler:^(GMSPolyline *polyline1) 
     { 
     if(polyline1) 
      polyline1.map = mapView_; 
     }]; 
    } 


- (void)fetchPolylineWithOrigin:(CLLocationCoordinate2D)origin destination:(CLLocationCoordinate2D)destination completionHandler:(void (^)(GMSPolyline *))completionHandler 
{ 
NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.latitude, origin.longitude]; 
NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.latitude, destination.longitude]; 
NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?"; 
NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving", directionsAPI, originString, destinationString]; 
NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString]; 


NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler: 
              ^(NSData *data, NSURLResponse *response, NSError *error) 
              { 
               dispatch_async(dispatch_get_main_queue(), ^{ 
                NSError *error; 
               NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
               if(error) 
               { 
                if(completionHandler) 
                 completionHandler(nil); 
                return; 
               } 

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

               GMSPolyline *polyline = nil; 

               if ([routesArray count] > 0) 
               { 
                NSDictionary *routeDict = [routesArray objectAtIndex:0]; 
                NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"]; 
                NSString *points = [routeOverviewPolyline objectForKey:@"points"]; 
                GMSPath *path = [GMSPath pathFromEncodedPath:points]; 
                polyline = [GMSPolyline polylineWithPath:path]; 
                polyline.strokeColor = [UIColor redColor]; 
                polyline.strokeWidth = 5.0; 
               } 

               if(completionHandler) 
                completionHandler(polyline); 
              }); 
              }]; 
[fetchDirectionsTask resume]; 
}