2016-11-28 64 views
-1

我是新來的mapview,我想顯示用戶的當前位置,並獲取方向到我想修復目標的地圖,但源不是修復。 我使用CLLocation類獲取當前位置。 現在我想解決目標,並使其禁用和不可編輯。顯示用戶在ios中的位置和方向使用mkmapview

請給我一些提示如何做到這一點。

感謝,

+0

你只是想顯示在圖形頁面2的位置和你想要展現的特殊途徑2地址? –

+0

@HimanshuMoradiya是 –

+0

檢查此鏈接可能會幫助你。 [http://stackoverflow.com/questions/29319643/how-to-draw-a-route-between-two-locations-using-mapkit-in-swift] – Aravi

回答

0

首先讓你的視圖控制器實現MKMapViewDelegate協議和聲明的屬性,你將需要:

@property (nonatomic, retain) MKMapView *mapView; //this is your map view 
@property (nonatomic, retain) MKPolyline *routeLine; //your line 
@property (nonatomic, retain) MKPolylineView *routeLineView; 

在viewDidLoad中然後(例如,或任何你初始化)

//初始化您的地圖視圖並將其添加到您的視圖層次結構中 - 將其委託設置爲se LF *

CLLocationCoordinate2D coordinateArray[2]; 
coordinateArray[0] = CLLocationCoordinate2DMake(lat1, lon1); 
coordinateArray[1] = CLLocationCoordinate2DMake(lat2, lon2); 


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

[self.mapView addOverlay:self.routeLine]; 

然後實現MKMapViewDelegate的方法 - (MKOverlayView *)的MapView:viewForOverlay:

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

     } 

     return self.routeLineView; 
    } 

    return nil; 
}