3

I need to display a MKMapView with more than 4 locations with different Annotations and a route connecting the locations. I have tried to display the multiple locations inside a MKMapView but i still not able to find out on how to connect the locations with a route. I am also trying to get this checked if i have implemented it in a right way. I have created a "CLLocationCoordinate2D" and then added a lat and long similarly for 4 points. I have created a custom object which implements MKAnnotation and returning a location .

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;
}

My concern here is i need to create a Annotation Point for every Location. Is there any alternative that i can load all the points at a single place. And another difficulty here is the route connecting all the multiple points. Any help on this? Thanks a lot

Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75

1 Answers1

3

The way you are adding the annotations is fine.
Not sure what your concern is and what you mean by "all the points at a single place".
If you want pins/annotations at several places, you have to create a separate annotation object for each place.

Drawing a route connecting those locations requires creating an overlay (not an "annotation").
You want to add an MKPolyline to the map for which you will specify the list of coordinates.

To draw the polyline, you don't need to also add annotations at each coordinate (but you could if you want to).

Creating and adding an MKPolyline and its corresponding MKPolylineView is very similar to MKPolygon and MKPolygonView. See this question for an example:

iPhone MKMapView - MKPolygon Issues

Community
  • 1
  • 1
  • Thanks for the response. The reason why i said it as a concern was i was actually allocating the AnnotationPoint object for every location. Considering that there are about 50 locaiton points, we need to allocate the objects 50 times. So i was just trying to figure it out if we can optimize the allocation of objects. Anyhow thanks again. Can you please breif about adding the route between the multiple points? – Pradeep Reddy Kypa Oct 04 '12 at 15:38
  • If you just want the line going through every point, you don't need to also add the annotations at each point. You only need to give the C array of coordinates to MKPolyline and it will draw the line through those points. If still not clear, let me know. –  Oct 04 '12 at 15:48
  • I am able to draw a line connecting the multiple pins but the problem here is the line connecting the pins doesn't really connect the roads in the map. It just directly connects different points midway between the roads..Just any help any on how to implement drawing the route between points through roads would be helpful – Pradeep Reddy Kypa Oct 05 '12 at 17:50
  • The polyline only draws the lines based on the given coordinates (it doesn't know about the roads that happen to appear under it). You'll have to use/find an api that returns turn-by-turn coordinates and create a polyline from those coordinates. A complication in iOS 6 is to find an api that is ok to use in Apple Maps. If it's not a requirement to show the route _within_ your app, you can just call the Maps app and let it show the route. –  Oct 05 '12 at 18:02
  • You would use - (void)calculateDirectionsWithCompletionHandler:(MKDirectionsHandler)completionHandler to return the route between each pair of points. If you are wanting to work out the optimal order to use to visit multiple points, that's a trickier problem, but you would use - calculateETAWithCompletionHandler: to tell you the time between each possible pair. – Peter Johnson Jun 25 '15 at 23:46
  • Anybody has swift code about above questions answer? – Emre Değirmenci Mar 07 '20 at 13:07