首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mkpolyline未被绘制

Mkpolyline未被绘制
EN

Stack Overflow用户
提问于 2015-03-15 16:04:53
回答 1查看 775关注 0票数 0

很明显,这条路线正在制定之中,但没有画出一条折线。我能够找到总距离,并验证了我们使用的坐标不是(0,0)。委托是否有问题,因为似乎addOverlay和addAnnotation (在下面所示的自定义方法中调用createAndAddAnnotationForCoordinate)方法都不起作用?

代码语言:javascript
复制
-(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方法,

代码语言:javascript
复制
 -(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{

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


}

我们重写的method委托方法,

代码语言:javascript
复制
-(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;
}

如果地址一位于NJ中,地址二位于CA中,则输出:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 01:06:42

罗恩,我怀疑你的折线(mainRoute.polyline) -你的rendererForOverlay看起来几乎完全像我正在成功使用的。除非出现非常基本的错误,比如没有设置MKMapView委托,或者将其设置为其他对象,否则我几乎可以肯定添加到映射中的polyline不包含有效的CLLocationCoordinate2D结构。

在斯威夫特,创造就像

代码语言:javascript
复制
    var coordinatePtr = UnsafeMutablePointer<CLLocationCoordinate2D>(track2DCoordinates)
    let trackPolygon = MKPolyline(coordinates: coordinatePtr, count: track2DCoordinates.count)
    mapView.removeOverlays(mapView.overlays)
    mapView.addOverlay(trackPolygon)

首先要验证您确实有一个有效的MKPolyline。

我也不确定你的middlePoint计算。

代码语言:javascript
复制
MKMapPoint middlePoint = mainRoute.polyline.points[mainRoute.polyline.pointCount/2];

这类事情现在可能有效,但在Swift中,您需要更加小心用作索引的数据类型。如果你有一个奇数点,或者零呢?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29062830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档