使用地理位置查找ios设备的当前位置

时间:2018-05-07 12:22:56

标签: ios objective-c geolocation

我想检查用户设备位置是否在印度,如果位置不是印度,则用户无法使用该应用,我使用区域设置进行了操作,但我想使用GeoLocation进行操作???

1 个答案:

答案 0 :(得分:2)

<强>目标C

CLLocation *location = [[CLLocation alloc]
                            initWithLatitude:+38.4112810
                            longitude:-122.8409780f];
CLGeocoder*myGeocoder = [[CLGeocoder alloc] init];

[myGeocoder
     reverseGeocodeLocation:location
     completionHandler:^(NSArray *placemarks, NSError *error) {

 if (error == nil && placemarks.count > 0){ 

      CLPlacemark *placemark = placemarks[0];

     NSLog(@"Country = %@", placemark.country); 

     NSLog(@"Postal Code = %@", placemark.postalCode);

     NSLog(@"Locality = %@", placemark.locality);
 }
 else if (error == nil && placemarks.count == 0){

     NSLog(@"No results were returned.");

  }
   else if (error != nil) {

      NSLog(@"An error occurred = %@", error);

    }
}];

<强>夫特

func gecode(location:CLLocation)
{

    let geoCoder = CLGeocoder()

    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        if(error != nil)
        {
            return
        }

        if let placeMark = placemarks?[0] {

            if let country = placeMark.addressDictionary!["Country"] as? String {

                if country == "India" {

                    //
                }

            }

        }

    })

}