iOS – Détecter une place avec les coordonnées GPS

est-il possible de détecter un lieu avec des coordonnées GPS? (autant que possible sans carte) par exemple, vérifiez si la longitude et la latitude sont égales ou proches de celles-ci:

44 35′25″n 104 42′55″w or 44.5903 -104.7153 

alors une alerte apparaît et affiche un message! Merci .

Vous pouvez mesurer la distance entre votre location «codé en dur» et l'location actuel des users en suivant:

 CLLocation *hardcoded_location = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1]; CLLocation *new_location = newLocation; CLLocationDistance distance = [hardcoded_location distanceFromLocation:new_location]; 

Alors vous pourriez faire:

 if (distance < some_value) { [[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; } 

Voir https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CoreLocationDataTypesRef/Reference/reference.html pour plus d'informations.

METTRE À JOUR:

typedef double CLLocationDistance :

Une mesure de distance (en mètres) à partir d'un location existant.

Ce code sera utilisé pour résoudre votre problème.

 CLLocation *currentLocation = newLocation; if (currentLocation != nil) NSLog(@"longitude = %.8f\nlatitude = %.8f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude); // stop updating location in order to save battery power [locationManager stopUpdatingLocation]; // Reverse Geocoding NSLog(@"Resolving the Address"); // “reverseGeocodeLocation” method to translate the locate data into a human-readable address. // The reason for using "completionHandler" ---- // Instead of using delegate to provide feedback, the CLGeocoder uses “block” to deal with the response. By using block, you do not need to write a separate method. Just provide the code inline to execute after the geocoding call completes. [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; // strAdd -> take bydefault value nil NSSsortingng *strAdd = nil; if ([placemark.subThoroughfare length] != 0) strAdd = placemark.subThoroughfare; if ([placemark.thoroughfare length] != 0) { // strAdd -> store value of current location if ([strAdd length] != 0) strAdd = [NSSsortingng ssortingngWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]]; else { // strAdd -> store only this value,which is not null strAdd = placemark.thoroughfare; } } if ([placemark.postalCode length] != 0) { if ([strAdd length] != 0) strAdd = [NSSsortingng ssortingngWithFormat:@"%@, %@",strAdd,[placemark postalCode]]; else strAdd = placemark.postalCode; } if ([placemark.locality length] != 0) { if ([strAdd length] != 0) strAdd = [NSSsortingng ssortingngWithFormat:@"%@, %@",strAdd,[placemark locality]]; else strAdd = placemark.locality; } if ([placemark.administrativeArea length] != 0) { if ([strAdd length] != 0) strAdd = [NSSsortingng ssortingngWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]]; else strAdd = placemark.administrativeArea; } if ([placemark.country length] != 0) { if ([strAdd length] != 0) strAdd = [NSSsortingng ssortingngWithFormat:@"%@, %@",strAdd,[placemark country]]; else strAdd = placemark.country; } 

Oui, vous pouvez détecter quand l'appareil est proche d'un endroit particulier (lat, lng) mais vous devez mentionner à quelle distance. Vous ne pouvez pas faire une simple addition et soustraction avec des valeurs de lat long. Vous devez spécifier un rayon ou une window.

 distanceFromCurrentLocation = [userLocation distanceFromLocation:destinationlocation]/convertToKiloMeter; if(distanceFromCurrentLocation < yourHigherBound && distanceFromLocation > yourLowerBound) { NSLog(@"yes, this place is inside my circle"); //Do the geocoding mentioned by others after this to get place name or country or whatever } else { NSLog(@"Oops!! its too far"); } 

Une bonne lecture, https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

Utilisez cette méthode

 @interface YourClass : UIViewController { CLGeocoder *geocoder; } @end -(void)viewDidLoad { geocoder = [[CLGeocoder alloc] init]; CLLocationCoordinate2D location; location.latitude = 44.5903f; location.longitude = -104.7153f; [self startGeocoderWithLocation:location]; } -(void)startGeocoderWithLocation:(CLLocationCoordinate2D)location { CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude]; [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { if (error == nil && [placemarks count] > 0) { CLPlacemark *placemark = [placemarks lastObject]; NSLog(@"%@",placemark.addressDictionary); // you can use placemark.thoroughfare, placemark.locality, etc } else { //Error in finding location } } ]; } 

Espère que ça pourrait aider