iphone - 在 iPhone MapView 上显示当前位置和所需位置之间的路线

标签 iphone path mkmapview android-mapview routes

我想在 MKMapView 上显示当前位置和所需位置之间的路线作为注释。

最好的方法是什么?

最佳答案

///在.h中 添加委托(delegate)MKMapViewDelegate

///在.m文件中

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
{

     CLLocationCoordinate2D newcordinate =   newLocation.coordinate;
     CLLocationCoordinate2D oldcordinate =   oldLocation.coordinate;

       MKMapPoint * pointsArray =
               malloc(sizeof(CLLocationCoordinate2D)*2);

           pointsArray[0]= MKMapPointForCoordinate(oldcordinate); 
           pointsArray[1]= MKMapPointForCoordinate(newcordinate);    

            MKPolyline *  routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
           free(pointsArray);

            [MapView addOverlay:routeLine];  //MkMapView declared in .h
}

//MKMapViewDelegate

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView* overlayView = nil;


          MKPolylineView  * _routeLineView = [[[MKPolylineView alloc]                   initWithPolyline:self.routeLine] autorelease];
            _routeLineView.fillColor = self.PathColor;
           _routeLineView.strokeColor = self.PathColor;
          _routeLineView.lineWidth = 15;
            _routeLineView.lineCap = kCGLineCapSquare;


            overlayView = _routeLineView;

            return overlayView;

}

关于iphone - 在 iPhone MapView 上显示当前位置和所需位置之间的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8604619/

相关文章:

iphone - init 中设置的属性在 viewDidLoad 之后立即变为 nil?

iphone - 在 cocoa/iPhone 中将纪元时间转换为 NSDate

iPhone以编程方式读取代理设置

macos - 在 OSX 中永久设置 PATH 环境变量

path - 如何获取 NUnit nunit-console.exe 的路径

cocoa - 解析 NSURL 中的所有符号链接(symbolic link)

ios - Mapkit map View 在第一次尝试时未显示

ios - 将视点转换为 MKMapView 坐标

ios8 UIImage imageNamed : crash

ios - map 上的折线不可见