它们都是对应出现的。
这个类是用来在地图上面画线的,一般用在记录用户的轨迹路线。
下面我简单用几个点来画下
_points=[[NSMutableArrayalloc]init];
CLLocation*location1 = [[CLLocationalloc]initWithLatitude:30.6734
longitude:104.0644];
CLLocation*location2 = [[CLLocationalloc]initWithLatitude:30.6834
longitude:104.0644];
CLLocation*location3 = [[CLLocationalloc]initWithLatitude:30.6934
longitude:104.0644];
[_pointsaddObject:location1];
[_pointsaddObject:location2];
[_pointsaddObject:location3];
[self configureRoutes];
- (void)configureRoutes
{
// define minimum, maximum points
MKMapPointnorthEastPoint =MKMapPointMake(0.f,0.f);
MKMapPointsouthWestPoint =MKMapPointMake(0.f,0.f);
// create a c array of points.
MKMapPoint* pointArray =malloc(sizeof(CLLocationCoordinate2D) *_points.count);
// for(int idx = 0; idx < pointStrings.count; idx++)
for(intidx =0; idx <_points.count; idx++)
{
CLLocation*location = [_pointsobjectAtIndex:idx];
CLLocationDegreeslatitude= location.coordinate.latitude;
CLLocationDegreeslongitude = location.coordinate.longitude;
// create our coordinate and add it to the correct spot in the array
CLLocationCoordinate2Dcoordinate =CLLocationCoordinate2DMake(latitude, longitude);
MKMapPointpoint =MKMapPointForCoordinate(coordinate);
// if it is the first point, just use them, since we have nothing to compare to yet.
if(idx ==0) {
northEastPoint = point;
southWestPoint = point;
}else{
if(point.x> northEastPoint.x)
northEastPoint.x= point.x;
if(point.y> northEastPoint.y)
northEastPoint.y= point.y;
if(point.x< southWestPoint.x)
southWestPoint.x= point.x;
if(point.y< southWestPoint.y)
southWestPoint.y= point.y;
}
pointArray[idx] = point;
}
if(self.routeLine) {
[self.mapViewremoveOverlay:self.routeLine];
}
self.routeLine= [MKPolylinepolylineWithPoints:pointArraycount:_points.count];
// add the overlay to the map
if(nil!=self.routeLine) {
[self.mapViewaddOverlay:self.routeLine];
}
free(pointArray);
}
最后同样要用代理方法判断是MKPolyline,然后设置填充颜色,和线宽
- (MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id)overlay
{
if([overlayisKindOfClass:[MKPolylineclass]])
{
MKPolylineView*lineview=[[MKPolylineViewalloc]initWithOverlay:overlay];
lineview.lineCap=kCGLineCapRound;
lineview.strokeColor= [UIColorblueColor];
lineview.fillColor= [UIColorblueColor];
lineview.lineWidth=8.f;
lineview.layer.shouldRasterize=YES;
returnlineview;
}else{
returnnil;
}
}