簡體   English   中英

如何根據用戶對iOS中某個固定點的接近程度來限制它們?

[英]How can I limit user actions based on their proximity to a fixed point in iOS?

我想我想做的應該很簡單,但不知道如何搜索!

我想做的是:用戶將為音樂投票,但只有在距固定位置(經緯度)2公里的情況下,用戶才能投票。

您能否建議一些鏈接來幫助我實現這一目標?

干杯。

像這樣

@interface YourLocationViewController : UIViewController <CLLocationManagerDelegate> 
CLLocationManager *locationManager;

...

/**
 * Start tracking updates for location. 
 * Call this from viewLoad or something.
 */
-(void) trackUpdates {

    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    self.locationManager.delegate = self;

    /* Pinpoint our location with the following accuracy:
     *
     *     kCLLocationAccuracyBestForNavigation  highest + sensor data
     *     kCLLocationAccuracyBest               highest     
     *     kCLLocationAccuracyNearestTenMeters   10 meters   
     *     kCLLocationAccuracyHundredMeters      100 meters
     *     kCLLocationAccuracyKilometer          1000 meters 
     *     kCLLocationAccuracyThreeKilometers    3000 meters
     */
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

    /* Notify changes when device has moved x meters.
     * Default value is kCLDistanceFilterNone: all movements are reported.
     */
    self.locationManager.distanceFilter = 10.0f;

    // update location
    if ([CLLocationManager locationServicesEnabled]){
        [self.locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    CLLocationDistance meters = [newLocation distanceFromLocation:fixedPoint];
    if (meters>2000){
        // drink a shot
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM