我想在地图中的多个地址或位置设置引脚,然后在此引脚点之间画一条线。 Pins和Line将显示在地图中。我找到一个代码,只显示一些位置的引脚..这是我的代码。
- (void)viewDidLoad {
[super viewDidLoad];
mapView.delegate=self;
NSMutableArray* annotations=[[NSMutableArray alloc] init];
for (int i =1; i <=10; i++) {
CLLocationCoordinate2D theCoordinate1;
if (i==1) {
theCoordinate1.latitude = 37.786996;
theCoordinate1.longitude = -122.419281;
}
if (i==2) {
theCoordinate1.latitude = 37.810000;
theCoordinate1.longitude = -122.477989;;
}
if (i==3) {
theCoordinate1.latitude = 37.80000;
theCoordinate1.longitude = -122.407989;
}
if (i==4) {
theCoordinate1.latitude = 37.82000;
theCoordinate1.longitude = -122.407989;
}
MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
[mapView addAnnotation:myAnnotation1];
[annotations addObject:myAnnotation1];
}
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapView.visibleMapRect = flyTo;
}
我想在这些要点之间画一条线。最后它看起来像一条路径,并且引脚正站在这条路径或线路上。 谢谢你的进步。
答案 0 :(得分:0)
例如
CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(31.484137685, 120.371875243);
CLLocationCoordinate2D coord2 = CLLocationCoordinate2DMake(31.484044745, 120.371879653);
points[0] = MKMapPointForCoordinate(coord1);
points[1] = MKMapPointForCoordinate(coord2);
MKPolyline *line = [MKPolyline polylineWithPoints:points count:2];
[myMap addOverlay: line];
您应该在下面添加此代码。
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *lineview=[[[MKPolylineView alloc] initWithOverlay:overlay] autorelease];
lineview.strokeColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];
lineview.lineWidth=2.0;
[arr release];
return lineview;
}
}