iOS - Detect a place with GPS coordinates

2019-08-14 15:09发布

is there any way to detect a place with GPS coordinates ? (as much as possible without map ) for example check if longitude and latitude is equal or near to these :

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

then an alert pops up and display a message ! Thanks .

4条回答
Fickle 薄情
2楼-- · 2019-08-14 15:42

Yes you can detect when the device is near to a particular location(lat,lng) but you have to mention how near. You cannot do simple plain addition and subtraction with lat long values. You have to specify a radius or a 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");
}

A good read, https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

查看更多
戒情不戒烟
3楼-- · 2019-08-14 15:43

You can measure the distance between your "hardcoded" location and current users location with following:

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

CLLocationDistance distance = [hardcoded_location distanceFromLocation:new_location];

Then you could do:

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

See https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CoreLocationDataTypesRef/Reference/reference.html for more info.

UPDATE:

typedef double CLLocationDistance:

A distance measurement (in meters) from an existing location.

查看更多
beautiful°
4楼-- · 2019-08-14 15:43

This code will be use full for solving your problem.

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
        NSString *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 = [NSString stringWithFormat:@"%@, %@",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 = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]];
            else
                strAdd = placemark.postalCode;
        }

        if ([placemark.locality length] != 0)
        {
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]];
            else
                strAdd = placemark.locality;
        }

        if ([placemark.administrativeArea length] != 0)
        {
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]];
            else
                strAdd = placemark.administrativeArea;
        }

        if ([placemark.country length] != 0)
        {
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]];
            else
                strAdd = placemark.country;
        }
查看更多
成全新的幸福
5楼-- · 2019-08-14 15:51

Use this method

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

Hopes it might help

查看更多
登录 后发表回答