我想在 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/