簡體   English   中英

尚未繪制Mkpolyline

[英]Mkpolyline is not being drawn

顯然已在制作路線,但未繪制折線。 我能夠找到總距離,並已驗證我們使用的坐標不是(0,0)。 委托有什么問題,因為看來addOverlay和addAnnotation(在下面顯示的自定義方法中稱為createAndAddAnnotationForCoordinate調用)方法都不起作用?

-(void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view.

//generates map view
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 
self.view.bounds.size.height)];
//setting delegate to self is not fixing the error
mapView.delegate = self;


mapView.mapType = MKMapTypeStandard;




//converting CLLocationCoordinate2D to MKPlacemark
MKPlacemark *startPlacemark = [[MKPlacemark alloc]
initWithCoordinate: addressOneCoords addressDictionary:

[NSDictionary dictionaryWithObjectsAndKeys:nil]];
MKPlacemark *endPlacemark = [[MKPlacemark alloc]initWithCoordinate: addressTwoCoords addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:nil]];

//converting MKPlacemark to MKMapItem
MKMapItem *start = [[MKMapItem alloc ]initWithPlacemark:startPlacemark];

MKMapItem *end = [[MKMapItem alloc]initWithPlacemark:endPlacemark];


MKDirectionsRequest *request = [MKDirectionsRequest new];
[request setSource:start];
[request setDestination:end];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
request.requestsAlternateRoutes = YES;

//Just to check if the coordinates were transferred successfully between view controllers and they did transfer successfully
NSLog(@"address one lat is %f",addressOneCoords.latitude);
NSLog(@"address one lon is %f",addressOneCoords.longitude);
NSLog(@"address two lat is %f",addressTwoCoords.latitude);
NSLog(@"address two lon is %f",addressTwoCoords.longitude);

MKDirections *directions = [[MKDirections alloc]initWithRequest:request];

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse response, NSError error){
    //if the route can't be created
    if(error){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Unable to create route" message:@"Go back to check if addresses are valid" delegate:nil cancelButtonTitle:@"Ok"otherButtonTitles:nil];
        [alert show];
    }
    else{

        [mapView removeOverlays:self.mapView.overlays];

        MKRoute *mainRoute = [response.routes firstObject];



        routeLine = mainRoute.polyline;


        if(routeLine){
            [self.mapView removeOverlay:routeLine];
        }

        //the addOverlay method is not drawing the polyline
        [self.mapView addOverlay: routeLine level:MKOverlayLevelAboveRoads];

        //proof that route is being created successfully
        NSLog(@"Total distance is %f", mainRoute.distance);

        MKMapPoint middlePoint = mainRoute.polyline.points[mainRoute.polyline.pointCount/2];

        //also, the addannotation method is not being called either it seems like
        [self createAndAddAnnotationForCoordinate:MKCoordinateForMapPoint(middlePoint)];


    }
}];


}

我們的createAndAddAnnotationForCoordinate方法,

 -(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{

MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"Point";
annotation.subtitle = @"subtitle";
[mapView addAnnotation:annotation];


}

我們重寫的mapviewdelegate方法,

-(MKOverlayRenderer )mapView:(MKMapView )mapView rendererForOverlay:(id<MKOverlay>)overlay{

if([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc]initWithOverlay:overlay];
renderer.strokeColor = [UIColor redColor];
renderer.lineWidth = 10.0f;

return renderer;
}

else
    return nil;
}

如果地址1在NJ中的某處,而地址2在CA中的某處,則輸出:

address one lat is 40.902599
address one lon is -74.407097
address two lat is 34.054435
address two lon is -118.253393
Total distance is 4459771.000000

羅恩,我懷疑您的折線( mainRoute.polyline )-您的rendererForOverlay看起來幾乎和我成功使用的折線完全一樣。 除非存在一些非常基本的錯誤,例如不設置MKMapView委托或將其設置為其他對象,否則我幾乎可以肯定,添加到地圖中的折線不包含有效的CLLocationCoordinate2D結構。

在Swift中,創作就像

    var coordinatePtr = UnsafeMutablePointer<CLLocationCoordinate2D>(track2DCoordinates)
    let trackPolygon = MKPolyline(coordinates: coordinatePtr, count: track2DCoordinates.count)
    mapView.removeOverlays(mapView.overlays)
    mapView.addOverlay(trackPolygon)

首先,確認您確實具有有效的MKPolyline。

我也不確定您的middlePoint計算。

MKMapPoint middlePoint = mainRoute.polyline.points[mainRoute.polyline.pointCount/2];

這種事情現在可能可以正常工作,但是在Swift中,您需要更加小心用作索引的數據類型。 如果您的分數是奇數或為零怎么辦?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM