- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
路线明明正在制作,但折线没有画出来。我能够找到总距离,并已验证我们使用的坐标不是 (0,0)。委托(delegate)有问题吗,因为 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;
}
如果地址一在 NJ 某处,地址二在 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
最佳答案
Ron,我怀疑你的折线 (mainRoute.polyline
) - 你的 rendererForOverlay 看起来几乎和我成功使用的一模一样。除非出现非常基本的错误,例如未设置 MKMapView 委托(delegate)或将其设置为其他对象,否则我几乎可以肯定您添加到 map 的多段线不包含有效的 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 中你需要更加小心用作索引的数据类型。如果您的点数为奇数或零怎么办?
关于ios - 未绘制 Mkpolyline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29062830/
我正在编写一个 iOS 5 应用程序,它实时跟踪用户的位置,在 MKMapView 上绘制他们的路线。每当获取 GPS 读数时,我都希望在当前位置和旧位置之间绘制一条多段线,最终形成用户旅行过的轨迹(
我正在尝试为相应的纬度/经度点数组绘制多条 MKPolylines。但是,当我将 addOverlay 添加到我的 mapView 时,点被绘制但未连接。 -(void)addAllRoutes:(N
我正在尝试跟踪用户的路线并绘制路线的线条,但是 addOverlay 只给我正确的点,但每个点之间没有连接。 -(void)viewWillAppear:(BOOL)animated{ sel
我目前有 MKAnnoations,上面有一个圆形图像,显示在 MKMapView 上。当我放下一个 MKPolyline 时,这些线在圆圈中间相遇,然后离开它。是否可以剪裁 MKPolyline 的
路线明明正在制作,但折线没有画出来。我能够找到总距离,并已验证我们使用的坐标不是 (0,0)。委托(delegate)有问题吗,因为 addOverlay 和 addAnnotation(在下面显示的
你好,我写了这段代码来在一些点之间画一条折线: var arrayToDraw: Array = [] var forpolyline: Array = [] var forpolyline2: CL
我有一条短线 (MKPolyline) 和一个自定义注释类 (MKPointAnnotaion)。现在我的点注释位于多段线的中点。但是,我希望每当触摸多段线上的任何点时都显示标注,类似于路由在 map
我正在尝试使用以下代码绘制 MKpolyline。 var points: [CLLocationCoordinate2D] = [] let locationDetails = myArray as
我在 map 上有两条折线: var polylineRoute : MKGeodesicPolyline! var polylineFlight : MKGeodesicPolyline! 我为它
我有一个应用程序,我在 map 上将路线显示为 MKPolyline。我进行了搜索,但找不到有关如何像 Apple 在 ios6 map 上那样显示自定义 MKPolyline 标注的示例代码: 有人
我需要在 MKPolyline 上找到等距位置来添加注释,如下所示。 下面给出了我获取 MKPolyline 上位置的函数,我有折线的开始和结束坐标值。但位置稍微偏离折线,如下图所示 我查找位置的函数
如何向 MKPolyline 的 boundingMapRect 添加插图以将其与 MKMapSnapshotter 一起使用? let polyLine = MKPolyline(coord
我想显示从我的位置到目的地的路线。我拿了这段代码 http://code.google.com/p/octomapkit并且我添加了一些日志消息。Go 正确获取了路线坐标(大约 103)。他们在路上并
我在 MKMapView 上有一个带有两个点(起点和终点)的 MKPolyline。有什么方法可以得到一些中间点(或坐标)以及线,或者将线分成许多段? 我想要这样的东西:http://i.imgur.
我有一个应用程序,其功能是在后台运行,收集位置数据('runkeeper' 风格的应用程序)。它可能会运行数小时,并收集数千分。 这些“运行”列在表格 View 中,选择后它将在 map 上重新绘制运
我正在尝试让我的 MKPolyline 显示出来,我想我几乎一切都正确,但我一定遗漏了一些东西。没有错误代码,否则 map 显示正常,我可以放下注释图钉。 .h @interface SatFinde
我正在努力学习MapKit并添加一些 MKPolyLine作为覆盖。几个问题: MKPolyLine 之间有什么区别?和 MKPolyLineView .什么时候用哪个? 对于 MKPolyLine
我正在尝试创建一个应用程序,其功能之一是在用户移动时画线。 这是类 class traceuserViewController: UIViewController,CLLocationManagerD
如何检查折线是否已添加到 map ? 我已经尝试了下面的代码,但它似乎不起作用 for (MKPolyline *feature1 in self.mapView.overlays) {
我想获取 MKPolyline 路径的米数,这样我就可以告诉用户完成路径还剩多少米。 我一直在寻找可以做这件事的东西,但我找不到任何东西。 谢谢。 最佳答案 听起来很乏味,除非您想自己进行数学运算,否
我是一名优秀的程序员,十分优秀!