在iPhone的地图上的路径线绘制

时间:2016-06-27 12:09:36

标签: ios objective-c iphone google-maps google-maps-api-3

我在两个位置之间的地图上尝试绘制路径线。它没有在路上显示,就像路上两个地方标记之间的最短距离。

我在路上试过了。 我MKDirectionsRequest以及(它给出了错误,我搜索了一些地方,它显示这在印度不起作用。)

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

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

Apple的卫星,方向,导航服务在几个国家都没有,这就是为什么你没有得到适当的结果。

请查看Apple的此链接:http://www.apple.com/in/ios/feature-availability/

您最好在应用中使用Google地图来获取位置坐标。

https://developers.google.com/maps/documentation/ios-sdk/start此链接对您有所帮助