2014-02-26 3 views
1

GPS 좌표가있는 장소를 감지하는 방법이 있습니까? 예를 들어 검사에 대한 (지도없이 가능한 한) 경도와 위도가이 근처에 동일하거나 경우 :iOS - GPS 좌표가있는 장소를 감지합니다.

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

는 경고가 나타나고 메시지를 표시! 감사합니다. .

답변

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를 참조하십시오.

UPDATE :

typedef double CLLocationDistance (미터)

거리 측정 위치에서 기존.

+0

+1 도움이 될 희망! – satheeshwaran

+0

좋은 대답, 단 한가지 질문, 미터로 'some_value'라고 언급하면 ​​방금 1 미터에 대해 1000CM 값을 언급해야합니까? 또는 그것을 계산해야합니까? –

+0

@ Mc.Lover 1m은 1000이 아닌 100cm이고, 항상 미터로 계산합니다. – AlexWien

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

예. 기기가 특정 위치 (lat, lng) 근처에있는 경우를 감지 할 수 있습니다. 하지만 근처에 언급해야합니다. 위의 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

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

distanceFromLocation에 대한