viewForOverlay不会在正确的位置加载和缩放地图

时间:2012-06-14 22:06:53

标签: objective-c mkmapview mkoverlay

我试图在MKMapView上绘制一个点数组,但我无法使其工作。地图显示并缩放到正确的区域,但viewForOverlay永远不会触发。我搜索了一下,我无法找到任何可以解决问题的方法。下面是我的代码(数组已经变成了3分):

NSString *dataString;

dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];



pointsData = [dataString JSONValue];

CLLocationCoordinate2D *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 3);

//NSLog(@"Data: %@", pointsData);

/*for(int i = 0; i < [pointsData count];i++)
{
    CLLocationDegrees lat = [[[pointsData objectAtIndex:i] objectForKey:@"lat"] floatValue];
    CLLocationDegrees lon = [[[pointsData objectAtIndex:i] objectForKey:@"long"] floatValue];
    pointsArray[i] = CLLocationCoordinate2DMake(lat, lon);


}*/
CLLocationDegrees lat = 41.861838603628;
CLLocationDegrees lon = -87.623975872993;
pointsArray[0] = CLLocationCoordinate2DMake(lat, lon);
lat = 41.840592684542;
lon = -87.623230218887;
pointsArray[1] = CLLocationCoordinate2DMake(lat, lon);
lat = 41.877006515959;
lon = -87.624126076698;
pointsArray[2] = CLLocationCoordinate2DMake(lat, lon);


MKPolyline *busRoute = [MKPolyline polylineWithCoordinates:pointsArray count:3]; //[pointsData count]];

[mapView addOverlay:busRoute];



 [mapView setVisibleMapRect:busRoute.boundingMapRect animated:TRUE];




}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];

    view.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:0.5];
    view.lineWidth = 2.0;
    return view;

}

0 个答案:

没有答案