使用mkmapview显示ios中的用户位置和方向

时间:2016-11-28 06:28:55

标签: ios objective-c mkmapview

我是mapview的新手,我想显示用户的当前位置并获取我想要修复目的地的地图方向但是源不能修复。 我使用CLLocation类获取当前位置。 现在我想修复目的地并使其禁用而不能编辑。

请给我一些提示如何做到这一点。

谢谢,

1 个答案:

答案 0 :(得分: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中(例如,或初始化的任何地方)

//初始化地图视图并将其添加到视图层次结构中 - 将其委托设置为自我*

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;
}