我需要显示一个MKMapView,其中包含4个以上具有不同Annotations的位置以及连接这些位置的路径。我试图在MKMapView中显示多个位置,但我仍然无法找到如何通过路线连接位置。如果我以正确的方式实现它,我也试图检查这个。我已经创建了一个“CLLocationCoordinate2D”,然后为4个点添加了一个lat和long。我创建了一个自定义对象,它实现MKAnnotation并返回一个位置。
CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(40.7180583 ,-74.007109);
CLLocationCoordinate2D coordinate2 = CLLocationCoordinate2DMake(40.716355 ,-74.006816);
CLLocationCoordinate2D coordinate3 = CLLocationCoordinate2DMake(40.715281 ,-74.005485);
CLLocationCoordinate2D coordinate4 = CLLocationCoordinate2DMake(40.71559 ,-74.003114);
AnnotationPoints *location1 = [[AnnotationPoints alloc] initWithCoordinate:coordinate1];
AnnotationPoints *location2 = [[AnnotationPoints alloc] initWithCoordinate:coordinate2];
AnnotationPoints *location3 = [[AnnotationPoints alloc] initWithCoordinate:coordinate3];
AnnotationPoints *location4 = [[AnnotationPoints alloc] initWithCoordinate:coordinate4];
NSArray *poiArray = [[NSArray alloc] initWithObjects:location1,location2,location3,location4,nil];
[mapView addAnnotations:poiArray];
//Inside the Annotation Class initWithCoordinate Method is implemented this way:-
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(@"%f,%f",c.latitude,c.longitude);
return self;
}
我担心的是我需要为每个位置创建一个注释点。有没有替代方案,我可以在一个地方加载所有点。另一个困难是连接所有多个点的路线。对此有何帮助?非常感谢
答案 0 :(得分:3)
添加注释的方式很好 不确定你关心的是什么,你的意思是“一个地方的所有要点” 如果您想在多个位置使用引脚/注释,则必须为每个位置创建单独的注释对象。
绘制连接这些位置的路线需要创建叠加(不是“注释”)
您想要为地图添加MKPolyline
,您将为其指定坐标列表。
要绘制折线,您不需要在每个坐标处添加注释(但如果您愿意,也可以添加注释)。
创建和添加MKPolyline
及其对应的MKPolylineView
与MKPolygon
和MKPolygonView
非常相似。请参阅此问题以获取示例: