3

I am new to Xcode. I am developing a vehicle tracking app for that I need to display more annotation points simultaneously. For this I need to store the coordinates in the array but it shows the error: Sending CLLocationCoordinate2D to parameter of incompatible type
my code is:

NSString *urlMapString=[NSString stringWithFormat:@"http://logix.com/logix_webservice/mapvehiclelist.php?uid=20&format=json"];
NSURL *urlMap=[NSURL URLWithString:urlMapString];
NSData *dataMap=[NSData dataWithContentsOfURL:urlMap];
if(dataMap!=NULL){
   NSError *errorMap;
   NSMutableArray *coordinates = [[NSMutableArray alloc]init];
   NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap];
   NSArray *resultsMap = [jsonMap valueForKey:@"posts"];
   for(int count=1;count<resultsMap.count;count++)
      {
       NSDictionary *resMap = [[resultsMap objectAtIndex:count]valueForKey:@"post"];
       NSString *latOrgstring =[resMap valueForKey:@"latitude"];
       latitude=[latOrgstring doubleValue];
       NSString *longitudeString=[resMap valueForKey:@"longitude"];
       longitude=[longitudeString doubleValue];
       //Center
       CLLocationCoordinate2D center;
       center.latitude=latitude;
       center.longitude=longitude;
       [coordinates addObject:center]; //here it shows the error
      }
}

Kindly advice me to solve this problem.
Thank you...

Atur
  • 1,712
  • 6
  • 32
  • 42
iBeginner
  • 249
  • 4
  • 12
  • This is the same basic issue as your previous question. – rmaddy Jul 24 '14 at 06:42
  • 1
    BTW - are you really using Xcode 4? Apple won't accept such an app so if you plan to send your app to Apple, you must use Xcode 5. And with iOS 8 coming soon you should be working with Xcode 6 to support iOS 8. – rmaddy Jul 24 '14 at 06:44
  • @rmaddy kindly advice me to solve this, Thank you... – iBeginner Jul 24 '14 at 06:44
  • @rmaddy Now am planning to update my xcode.... – iBeginner Jul 24 '14 at 06:45
  • 1
    Please put some effort into this after reviewing the answer to your last question. You can't write an app by getting others to do your work for you every time you get stuck. Try to solve this yourself first. Make an attempt. If you, after trying, you are still stuck, post what you tried. – rmaddy Jul 24 '14 at 06:49
  • ya sir I have tried a lot after that only I post this & for your advice I will try more thank you – iBeginner Jul 24 '14 at 06:50
  • 1
    Well you don't appear to be learning. I told you in your previous question that those `struct`s could not be stored directly in an Objective-C collection, and you accepted the answer, however you didn't actually learn anything from it: http://stackoverflow.com/questions/24925546/error-assigning-to-cllocationcoordinate2d-from-incompatible-type-id – trojanfoe Jul 24 '14 at 06:54
  • @trojanfoe sorry am very new to objective-c i tried your code also for this error but i can't solve this.. – iBeginner Jul 24 '14 at 07:00

4 Answers4

2

CLLocationCoordinate2D center isn't an object. You can store only objects in NSArray. Use CLLocation for this.

CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
AlKozin
  • 904
  • 8
  • 25
  • Sorry could you please explain little brief about it? according the above code – iBeginner Jul 24 '14 at 06:53
  • @iBeginner CLLocationCoordinate2D is a C struct. If you want to store it in array you should use C array. But if you want to store location object in array you should convert it in to Obj C object. One of the options is to store CLLocationCoordinate2D in CLLocation object. Another way is to convert it to NSValue. You can see documentation for this methods here: https://developer.apple.com/library/ios/documentation/MapKit/Reference/NSValue_MapKit_additions/Reference/Reference.html – AlKozin Jul 24 '14 at 12:16
2

As already mentioned, you can only add objects to an NSArray.

CLLocationCoordinate2D is not an object -- it is a C struct.


The code you posted itself has one solution:
Create a NSDictionary with the latitude and longitude as key/value pairs and add the resulting dictionary object to the array.


Another approach is to create a CLLocation as shown in another answer.


A third idea is what @trojanfoe answered previously which is to wrap the struct in an NSValue.

However, note that there is a convenient NSValue addition specifically for MapKit that adds these two useful helper methods:

  • valueWithMKCoordinate: which returns an NSValue given a CLLocationCoordinate2D
  • MKCoordinateValue which returns a CLLocationCoordinate2D for the NSValue

For an example, see How to store CLLocationCoordinate2D?.


A final (at least in this answer) alternate approach which I would highly recommend instead of all the above is this...

  • Why do you want to store only the coordinates in an array?
  • Wouldn't you want to know what the coordinates are for (which vehicle, place, etc)?
  • Why not create a custom class that implements, say, the MKAnnotation protocol and has not only the coordinates (as a CLLocationCoordinate2D property) but also all the other information related to the coordinate? The class would be a subclass of NSObject<MKAnnotation>.
  • You could then conveniently access all the information in one place without using multiple arrays and trying to keep the objects in the same order so they all have the same index, etc.
  • You could then directly add these objects to an MKMapView since they implement MKAnnotation.
Community
  • 1
  • 1
1

CLLocationCoordinate2D is a C struct, so you need to put it in NSValue container at first.

An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object id references. Use this class to work with such data types in collections (such as NSArray and NSSet), key-value coding, and other APIs that require Objective-C objects.

[coordinates addObject:[NSValue value:&coordinate withObjCType:@encode(CLLocationCoordinate2D)]];
Max Niagolov
  • 684
  • 9
  • 26
1

Try this

CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[points addObject:[NSValue valueWithMKCoordinate:new_coordinate]];

Or

CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[coordinates addObject: location];

Can't u directly add coordinates to mapview that means Place MKAnnotation on Mapview, instead of taking coordinates to into array ?

SEE Below i commented with lines

for(int count=1;count<resultsMap.count;count++)
  {
   NSDictionary *resMap = [[resultsMap objectAtIndex:count]valueForKey:@"post"];
   NSString *latOrgstring =[resMap valueForKey:@"latitude"];
   latitude=[latOrgstring doubleValue];
   NSString *longitudeString=[resMap valueForKey:@"longitude"];
   longitude=[longitudeString doubleValue];
   //Center
   CLLocationCoordinate2D center;
   center.latitude=latitude;
   center.longitude=longitude;
    -----------------------------------------
  ////// Annotation is MKAnnotation Subclass
    Annotation * cartAnn = [Annotation new]; 
    cartAnn.coordinate = center; 
    [self.mapView addAnnotation: cartAnn];
  ----------------------------------------------------
  /////// [coordinates addObject:center]; //here it shows the error
  }
Sanju
  • 1,148
  • 11
  • 26