2016-06-24 9 views
0

私のプロジェクトでは、緯度と経度の助けを借りて2つの場所の間のルートパスを見つけました。私はこのiOS:2つの座標間のルートパスを取得する方法

enter image description here

のようにシミュレータ上で出力を取得しています。しかし、私はルートマップをしたい、このコードでは、それ

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [mapvw setDelegate:self];  
    [self mapp]; 
} 


-(void)mapp 
{ 
    CLLocationCoordinate2D coordinateArray[2]; 
    coordinateArray[0] = CLLocationCoordinate2DMake(28.6129, 77.2295); 
    coordinateArray[1] = CLLocationCoordinate2DMake(28.6562, 77.2410); 

    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2]; 
    [mapvw setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible 
    [mapvw addOverlay:self.routeLine]; 
} 


-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay 
{ 
    if(overlay == self.routeLine) 
    { 
     if(nil == self.routeLineView) 
     { 
      self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; 
      self.routeLineView.fillColor = [UIColor redColor]; 
      self.routeLineView.strokeColor = [UIColor redColor]; 
      self.routeLineView.lineWidth = 3; 

     } 

     return self.routeLineView; 
    } 

    return nil; 
} 

ために、次のコードを使用してい

直線ではなく道路による方向を示す。 最後の2日間で困っています。 ....

+0

Google MapまたはApple Map(MapKit - デフォルト)を使用していますか? –

+0

私はリンゴマップを使用しています – Abhi

+0

しかし、あなたがGoogleマップのユーザーであれば、2つの場所の間のルートを作成するのは非常に簡単です。私はリンゴマップを実装していません。 –

答えて

7

2つのlatの間にパスを描くには、以下のコードを使用します。

ViewController.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface ViewController : UIViewController<MKMapViewDelegate> 

@property (strong, nonatomic) IBOutlet MKMapView *myMapView; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@property (strong, nonatomic) MKPlacemark *destination; 
@property (strong,nonatomic) MKPlacemark *source; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    [self getDirections]; 

} 

-(void)getDirections { 

    CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(37.773972, -122.431297); 

    MKCoordinateRegion region; 
    //Set Zoom level using Span 
    MKCoordinateSpan span; 
    region.center = sourceCoords; 

    span.latitudeDelta = 1; 
    span.longitudeDelta = 1; 
    region.span=span; 
    [_myMapView setRegion:region animated:TRUE]; 

    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil]; 

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 
    annotation.coordinate = sourceCoords; 
    annotation.title = @"San Francisco"; 
    [self.myMapView addAnnotation:annotation]; 
    //[self.myMapView addAnnotation:placemark]; 

    _destination = placemark; 

    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:_destination]; 

    CLLocationCoordinate2D destCoords = CLLocationCoordinate2DMake(37.615223, -122.389977); 
    MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithCoordinate:destCoords addressDictionary:nil]; 

    MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init]; 
    annotation1.coordinate = destCoords; 
    annotation1.title = @"San Francisco University"; 
    [self.myMapView addAnnotation:annotation1]; 

    //[self.myMapView addAnnotation:placemark1]; 

    _source = placemark1; 

    MKMapItem *mapItem1 = [[MKMapItem alloc] initWithPlacemark:_source]; 

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init]; 
    request.source = mapItem1; 

    request.destination = mapItem; 
    request.requestsAlternateRoutes = NO; 

    MKDirections *directions = [[MKDirections alloc] initWithRequest:request]; 

    [directions calculateDirectionsWithCompletionHandler: 
     ^(MKDirectionsResponse *response, NSError *error) { 
      if (error) { 
       NSLog(@"ERROR"); 
       NSLog(@"%@",[error localizedDescription]); 
      } else { 
       [self showRoute:response]; 
      } 
     }]; 
} 

-(void)showRoute:(MKDirectionsResponse *)response 
{ 
    for (MKRoute *route in response.routes) 
    { 
     [_myMapView 
     addOverlay:route.polyline level:MKOverlayLevelAboveRoads]; 

     for (MKRouteStep *step in route.steps) 
     { 
      NSLog(@"%@", step.instructions); 
     } 
    } 
} 

#pragma mark - MKMapViewDelegate methods 

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay]; 
    renderer.strokeColor = [UIColor colorWithRed:0.0/255.0 green:171.0/255.0 blue:253.0/255.0 alpha:1.0]; 
    renderer.lineWidth = 10.0; 
    return renderer; 
} 

は、それはあなたを助けることを願っています!上記のコードであなたの要件に応じてlong latを変更してください。

+0

これはエラーを示しています:セレクタ 'addOverlay:level:'の既知のクラスメソッドがありません。 – Abhi

+0

私はlatとlongを使用しているときにルートを表示していません。あなたの緯度経度の唯一のルートを示す – Abhi

+0

アップルマップは一部の国では制限されています。 – Gati

関連する問題