2014-02-26 100 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 distanceFromLocation! – satheeshwaran

+0

好的答案,只是一個問題,如果提到'some_value'作爲米我應該剛剛提到1000CM值爲1米?或應該計算它? –

+0

@ Mc.Lover 1m是100cm不是1000,總是以米計算 – 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)但你必須提一下接近。你不能用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

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

公司希望它可以幫助