0

我想在地图中的多个地址或位置设置 pin,然后在这个 pin 点之间画一条线。引脚和线将显示在地图中。我找到了一个代码,它只显示某个位置的 pin .. 这是我的代码。.

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

}

我想在这些点之间画一条线。最后 它 看起来 像 一条 路径 , 并且 大头针 站在 这条 路径 或 线上 . 谢谢你提前。

4

1 回答 1

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;
    }
}
于 2013-09-24T08:30:44.860 回答