简体   繁体   English

为什么MKPolyline没有显示在MKMapView上?

[英]Why a MKPolyline doesn't show on the MKMapView?

I'm pretty new on programmin to develop iphone applications and i would like to know why the MKPolyline that I create with 2 MKMapPoints doesn't appears in the MKMapView that I insert on my view. 我是在Programmin上开发iPhone应用程序的新手,我想知道为什么我用2个MKMapPoints创建的MKPolyline没有出现在我插入视图的MKMapView中。 Here is my code: 这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];

map = [[MKMapView alloc] initWithFrame:self.view.bounds];


MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*4);

CLLocationCoordinate2D punto1;
punto1.latitude =39.468502;
punto1.longitude =-0.398469;


MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint.coordinate = punto1;
annotationPoint.title = @"Point 1";

MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc]init];
annotationPoint2.coordinate = CLLocationCoordinate2DMake(39.472312,-0.386453);
annotationPoint2.title = @"Point 2";


[map addAnnotation:annotationPoint];
[map addAnnotation:annotationPoint2];


pointsArray[0]= MKMapPointForCoordinate(punto1);

pointsArray[1]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.467011,-0.390015));

pointsArray[2]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.469926,-0.392118));

pointsArray[3]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.472312,-0.386453));

routeLine = [MKPolyline polylineWithPoints:pointsArray count:4];

free(pointsArray);

[map addOverlay:routeLine];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(39.467011,-0.392515), 1100, 1100);
[map setRegion:region];

[self.view insertSubview:map atIndex:0];

}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay

{

MKOverlayView* overlayView = nil;

MKPolylineView  * routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];


routeLineView.fillColor = [UIColor blueColor];

routeLineView.strokeColor = [UIColor orangeColor];

routeLineView.lineWidth = 3;

overlayView = routeLineView;

return overlayView;

}

The annotations are OK and they show correctly on the map. 注释可以,并且可以在地图上正确显示。 Hope someone can help, thanks!!! 希望有人能帮忙,谢谢!!!

The MKPolyline doesn't show because the map's delegate is not set. 由于未设置地图的delegate因此不会显示MKPolyline

The viewForOverlay delegate method won't get called if the map's delegate property is not set. 如果未设置地图的delegate属性,则不会调用viewForOverlay 委托方法。 Because the delegate method is never called, the MKPolylineView never gets created, etc... 因为永远不会调用委托方法, MKPolylineView永远不会创建MKPolylineView ,等等。

After creating the MKMapView , set its delegate : 创建MKMapView ,设置其delegate

map = [[MKMapView alloc] initWithFrame:self.view.bounds];
map.delegate = self;  // <-- add this



I'd like to mention a few other unrelated points: 我想提及其他一些无关的要点:

  • Since you are putting MKMapPoint values in the pointsArray , the malloc should use sizeof(MKMapPoint) instead of sizeof(CLLocationCoordinate2D) . 既然你把MKMapPoint值在pointsArraymalloc应该使用sizeof(MKMapPoint)代替sizeof(CLLocationCoordinate2D) It happens to work with the wrong code because both structs happen to be the same size. 由于两个结构碰巧大小相同,因此碰巧使用了错误的代码。 But you should still use the right code. 但是您仍然应该使用正确的代码。

  • MKPolyline also has a polylineWithCoordinates:count: method so you can pass CLLocationCoordinate2D instead of MKMapPoint structs. MKPolyline还具有polylineWithCoordinates:count:方法,因此您可以传递CLLocationCoordinate2D而不是MKMapPoint结构。 This can be easier to read, understand, and avoids having to convert from coordinates to mappoints. 这可以更容易阅读,理解,并且避免必须将坐标转换为地图点。

  • The MKPolylineView uses strokeColor only so setting the fillColor for it does nothing. MKPolylineView使用strokeColor只有这么设置fillColor为它什么都不做。

  • In the viewForOverlay delegate method, it's much better practice to use the overlay parameter that is passed into the method instead of the externally declared routeLine . viewForOverlay委托方法中,更好的做法是使用传递到方法中的overlay参数代替外部声明的routeLine This will be extremely important if you want to add more than one overlay. 如果要添加多个叠加层,这将非常重要。 You would also first check what kind of class overlay is and then create the appropriate view for it. 您还将首先检查哪种类overlay ,然后为其创建适当的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM