2

I am creating an app, that should display a route between two points.

MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:locations count:2];
[mapView addOverlay:routeLine];

The two locations are stored in the array "locations".

I am getting an error

Implicit conversion of Objective-C pointer type 'NSMutableArray *' to C pointer type 'CLLocationCoordinate2D *' requires a bridged cast

Please help me over this.
Thanks in advance.

chandru
  • 407
  • 1
  • 5
  • 26
  • What is the exact type of the objects stored in the `locations` array? –  Nov 22 '13 at 11:52
  • Annotations that contains map coordinates. – chandru Nov 22 '13 at 11:57
  • 1
    After making the changes suggested by the answer, did you also implement the viewForOverlay or rendererForOverlay delegate method? –  Nov 22 '13 at 12:24
  • No, I am a fresher I dont know, what is that nu – chandru Nov 22 '13 at 12:26
  • I highly recommend reading [Apple's Location and Maps Programming Guide](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html). –  Nov 22 '13 at 12:40
  • I used the code below, no error comes, but nothing happens – chandru Nov 22 '13 at 13:37
  • As I said, you also need to implement the viewForOverlay (rendererForOverlay in iOS 7+) method. See http://stackoverflow.com/a/16839813/467105 and http://stackoverflow.com/a/15612056/467105 for examples. –  Nov 22 '13 at 13:58
  • Thank u @AnnaKarenina, but it is creating a an overlay(line), I want draw a path between two points, can U please suggest me something for that, – chandru Nov 25 '13 at 05:10
  • For iOS 7, see http://stackoverflow.com/questions/19772900/is-there-a-way-to-get-directions-in-mkmapview-using-a-built-in-apple-api. For iOS 6 or earlier, you'll have to get the route coordinates from another source (like Google Directions) but you have to check whether that source allows their data to be displayed on an Apple map. –  Nov 25 '13 at 18:02
  • But, it is only for ios7, I have to do so that it will support from iOS5.0 – chandru Nov 26 '13 at 05:13

2 Answers2

2

This method requires a parameter of type CLLocationCoordinate2D array. You will have to make an array of type CLLocationCoordinate2D. Code will look some kind of this:

CLLocationCoordinate2D *coordsArray = malloc(sizeof(CLLocationCoordinate2D) * locations.count);

int i = 0;
for (CLLocation *loc in locations) {
    coordsArray[i] = loc.coordinate;
    i++;
}

MKPolyline * routeLine = [MKPolyline polylineWithCoordinates:coordsArray 
                        count:locations.count];

free(coordinateArray);

[mapView addOverlay:routeLine];
Saad
  • 8,857
  • 2
  • 41
  • 51
  • did you checked coords properly being filled? and ur points have proper values? – Saad Nov 22 '13 at 11:29
  • I have plotted the points well using story board, and when I click those annotations it will display its coordinates in the text boxes, so, no problem in plotting the points. – chandru Nov 22 '13 at 11:36
  • Actually, I didnt implemented the viewForOverlay method, that's why I got an error.Now I got it. Thank u – chandru Nov 26 '13 at 05:38
  • But, Polyline is drawing a straight line between the two points, but I want to draw a path between the two points. – chandru Nov 26 '13 at 05:40
0

polylineWithCoordinates:count: accepts an array of objects of type CLLocationCoord2D and count is number of objects in coord. check out this link. I read your comments below, and it seems like your objects in the array (locations) are of type MKAnnotations, while they should be CLLocationCoord2D. Have a look into this. Also the error says that one of the objects of this array (which you are providing) contains an object of type NSMutableArray. Maybe somewhere, you are adding wrong instances. Have a look at this too.

Let me know if this helps.

Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41
  • What I said is that, location array contains annotation objects, which contains the coordinates – chandru Nov 22 '13 at 13:27
  • Yes. But the K-Compiler doesn't understand that. You will need to directly put these coordinates in the array and not the objects(annotation objects) which have coordinates. :P – Dunes Buggy Nov 22 '13 at 13:29