Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding random MKAnnotations around Current Location in a MKMapView

I have an MKMapView that shows the current users location. When I click a button in the navigation bar, I want to randomly drop 10 MKAnnotation pins. These can be dropped anywhere, randomly, but only within the current visible map area and around the current location.

How would go about doing something like this? Is there a way to have a long/lat range that is around the users location, but within the map area? Then, I could randomly choose from this range?

Basically, I need to find what coordinates are available in the current MKCoordinateRegion.

Is there a better way to go about this?

like image 784
Nic Hubbard Avatar asked May 23 '26 17:05

Nic Hubbard


1 Answers

You can get your annotations using the distance between user location and other location

check out below code

#define DISTANCE_RADIUS     10.0    // in KM
-(NSArray *)findNearMe:(NSArray *)lounges {
NSMutableArray *arNearme = [NSMutableArray array];

CLLocation *currentLocation;
for(NSDictionary *d in lounges) 
{

currentLocation = [[CLLocation alloc] initWithLatitude:29.33891 longitude:48.077202];

    CGFloat latitude=[[d valueForKey:@"latitude"] floatValue];
    CGFloat longitude=[[d valueForKey:@"longitude"] floatValue];
    CLLocation *newPinLocation=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    double distanceValue=[currentLocation distanceFromLocation:newPinLocation]; 

    if(distanceValue/1000.0<=DISTANCE_RADIUS) {
        [arNearme addObject:d];
    }
}
return  arNearme;
}

It will returns you an array of range of 1 to 1000 km near user location. Used the annotations array and show your annotations in to map view with user location.

like image 103
Hiren Avatar answered May 25 '26 08:05

Hiren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!