dpalmajr
dpalmajr

Reputation: 537

MKMapView Latitude and Longitude Coordinates

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

Upvotes: 6

Views: 1825

Answers (2)

metaprogrammer
metaprogrammer

Reputation: 96

Canappi is based on a simple programming language, mdsl, that is used to generate Objective-C code for iOS. They have a map control that allows you to achieve what you need from a simple descriptor. As this example shows, you can use static push pin locations, or dynamic ones:

map venuMap (32,610,708,360) {
    Standard ;
    show user ;
    area .1,.1; 
    //location 30.275806,-97.740128 ('TX Capital' , '1200 N Congress');
    locationReference lat, long (sDetTitle, address);
}

The generated code is open, so you can just copy and paste it back into your app.

Upvotes: 0

user467105
user467105

Reputation:

Your original code was this:

double latitude = arcrandom()%20 +50;
double longitude = -107  + arcrandom()%10;

(Actually, you probably had arc4random, not arcrandom.)


The arc4random function returns an unsigned integer value.

Subtracting an integer (-107) from that value resulted in an overflow which gave values like 4294967189. That would definitely be an invalid longitude.

Instead of switching to rand (which the documentation says is a "bad random number generator"), use arc4random (which I believe is preferred) and force a floating point calculation by writing -107.0 instead of -107:

double latitude = arc4random()%20 +50;
double longitude = -107.0  + arc4random()%10;


An unrelated point is that if CLLocationCoordinate2DIsValid says NO, you are just doing a return without sending back any value (which you need to according to the method declaration). Either do return tempMapData; or continue;.

Upvotes: 3

Related Questions