Show route between current and desired location on iPhone MapView

前端 未结 3 1750
说谎
说谎 2020-12-28 11:24

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

What is the best way to do this?

3条回答
  •  不知归路
    2020-12-28 12:04

    ///in .h add delegate MKMapViewDelegate

    ///in .m file

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

提交回复
热议问题