I’m currently working with the MKMapView and I’m trying to get data to appear on screen. To accomplish this I’ve decided to right a small MapDataProvider that spits out an array of MKAnnotation objects – each containing a coordinate with random latitude and longitude values.
I’ve already made sure that my MKMapView is hooked up to my controller and the array of MKAnnotation objects are coming from my MapDataProvider correctly…but for some reason..when I try and specify coordinates in North America (ex. 48, -84)..nothing appears on the MKMapView.
After playing around I found out that any longitude value less than 0 gives me this issue.
I’ve tried verifying the coordinate value for each MKAnnotation object in my collection, but CLLocation2DIsValue() keeps returning false.
Question:
What range of values can I enter for latitude and longitude for a CLLocationCoordinate2D so my pins show up in North America?
To give a bit more context, here’s the method being invoked in the MapDataProvider:
+(NSArray *) getMockMapData{
NSMutableArray *tempMapData = [[NSMutableArray alloc] initWithCapacity:15];
for (int i=0; i< 15; i++) {
double latitude = rand()%20 +50;
double longitude = -107 + rand()%10;
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(latitude, longitude);
if(CLLocationCoordinate2DIsValid(coord) == NO)
continue;
[tempMapData addObject:[MockMapData
dataForValues:[@"Item " stringByAppendingString:[[NSNumber numberWithInt:i] description]]
subTitle:[@"Item " stringByAppendingString:[[NSNumber numberWithInt:i]description]]
coordinate:coord]];
}
return tempMapData;
}
Your original code was this:
(Actually, you probably had
arc4random, notarcrandom.)The
arc4randomfunction returns an unsigned integer value.Subtracting an integer (
-107) from that value resulted in an overflow which gave values like4294967189. That would definitely be an invalid longitude.Instead of switching to
rand(which the documentation says is a “bad random number generator”), usearc4random(which I believe is preferred) and force a floating point calculation by writing-107.0instead of-107:An unrelated point is that if
CLLocationCoordinate2DIsValidsaysNO, you are just doing areturnwithout sending back any value (which you need to according to the method declaration). Either doreturn tempMapData;orcontinue;.