iOS - 使用GPS坐标检测地点

时间:2014-02-26 08:53:01

标签: ios iphone objective-c gps

有没有办法检测有GPS坐标的地方? (尽可能没有地图)例如检查经度和纬度是否等于或接近这些:

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

然后会弹出一个警告并显示一条消息! 谢谢 。

4 个答案:

答案 0 :(得分:2)

您可以使用以下方法衡量“硬编码”位置与当前用户位置之间的距离:

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

CLLocationDistance distance = [hardcoded_location distanceFromLocation:new_location];

然后你可以这样做:

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

有关详细信息,请参阅https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CoreLocationDataTypesRef/Reference/reference.html

<强>更新

typedef double CLLocationDistance

距离现有位置的距离测量值(以米为单位)。

答案 1 :(得分:0)

此代码将用于解决您的问题。

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

答案 2 :(得分:0)

是的,您可以检测到设备何时接近特定位置(lat,lng),但您必须提及接近程度。您不能使用lat long值进行简单的加法和减法。您必须指定半径或窗口。

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

好读,https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

答案 3 :(得分:0)

使用此方法

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

希望它可以提供帮助