【问题标题】:Path line draw on map in iPhoneiPhone 在地图上绘制路径线
【发布时间】:2016-10-29 10:44:25
【问题描述】:

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

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

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

【问题讨论】:

  • 你能告诉我你做了什么吗?我是说代码?
  • 你在使用谷歌地图吗?
  • 我认为 snap to road 需要沿着路线绘制很多点才能有效地工作(不要相信它只适用于起点和终点位置)。
  • 最好的解决方案是找出MKDirectionRequest 不起作用的原因(您可以发布错误吗?)或者改为实施 Google Maps SDK 并使用此方向请求功能。

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


【解决方案1】:

您可以为此使用 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];
}

【讨论】:

    【解决方案2】:

    Apple 的卫星、方向、导航服务在多个国家/地区不存在,这就是您没有获得适当结果的原因。

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

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

    https://developers.google.com/maps/documentation/ios-sdk/start这个链接会对你有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      相关资源
      最近更新 更多