Latitude and Longitude Coordinates MKMapView

I am currently working with MKMapView and I am trying to make the data appear on the screen. To do this, I decided to choose a small MapDataProvider, which splashes out an array of MKAnnotation objects - each of which contains a coordinate with random values ​​of latitude and longitude.

I have already made sure that my MKMapView is connected to my controller, and the array of MKAnnotation objects comes from my MapDataProvider correctly ... but for some reason .. when I try to specify coordinates in North America (for example, 48, -84) .. nothing is displayed in MKMapView.

After the game, I found out that any value of longitude less than 0 gives me this problem.

I tried to check the coordinate value for each MKAnnotation object in my collection, but CLLocation2DIsValue () continues to return false.

Question

What range of values ​​can I enter for latitude and longitude for CLLocationCoordinate2D so that my contacts appear in North America?

To give a little more context, here is the method called in 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; } 
+6
source share
2 answers

Your original code:

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

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


The arc4random function returns an unsigned integer.

Subtracting an integer ( -107 ) from this value resulted in an overflow that produced values ​​such as 4294967189 . This will definitely be an unacceptable longitude.

Instead of switching to rand (which, according to the documentation, is a "random number generator"), use arc4random (which I believe is preferable) and force floating point calculations, I write -107.0 instead of -107 :

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


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

+3
source

Canappi is based on the simple mdsl programming language, which is used to generate Objective-C code for iOS. They have a map control that allows you to get what you need from a simple handle. As shown in this example, you can use static contact locations or dynamic:

 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 simply copy and paste it back into your application.

0
source

Source: https://habr.com/ru/post/911163/


All Articles