Issue
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?
Solution
///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 <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;
}
Answered By - Andrew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.