本文介绍了在iPhone MapView上显示当前位置和所需位置之间的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show a route on a MKMapView between the current location and a desired location as an annotation.

最好的方法是什么?

推荐答案

/// in .h
add delegate MKMapViewDelegate

///in .h add 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 MapView上显示当前位置和所需位置之间的路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:16