2011-06-29 18 views
34

के साथ अपना वर्तमान स्थान कैसे ढूंढें मुझे CoreLocation के साथ अपना वर्तमान स्थान ढूंढना होगा, मैंने कई तरीकों की कोशिश की लेकिन अब तक मेरे CLLocationManager ने केवल 0 के लौटा दिए हैं .. (0.000.00.000)।कोरलोकेशन

आयात:

#import <CoreLocation/CoreLocation.h> 

घोषित:

यहाँ मेरी कोड ( काम करने के लिए अद्यतन) है

IBOutlet CLLocationManager *locationManager; 
IBOutlet UILabel *latLabel; 
IBOutlet UILabel *longLabel; 

कार्य:

- (void)getLocation { //Called when needed 
    latLabel.text = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude]; 
    longLabel.text = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude]; 
} 

- (void)viewDidLoad { 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 
} 

उत्तर

77

देख आप अपने स्थान CoreLocation इस तरह का उपयोग कर पा सकते हैं:

आयात CoreLocation:

#import <CoreLocation/CoreLocation.h> 

घोषित CLLocationManager:

CLLocationManager *locationManager; 

प्रारंभ +०१२३५३५६५५९ viewDidLoad मेंऔर एक समारोह है कि एक NSString के रूप में वर्तमान स्थान return कर सकते हैं बनाने के लिए:

- (NSString *)deviceLocation { 
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; 
} 

- (void)viewDidLoad 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 
} 

और deviceLocation समारोह बुला स्थान के रूप में उम्मीद वापस आ जाएगी:

NSLog(@"%@", [self deviceLocation]); 

यह सिर्फ एक उदाहरण है। उपयोगकर्ता के लिए तैयार होने के बिना CLLocationManager शुरू करना एक अच्छा विचार नहीं है। और, ज़ाहिर है, CLLocationManager के बाद latitude और longitude प्राप्त करने के लिए locationManager.location.coordinate का उपयोग किया जा सकता है।

बिल्ड चरण टैब (Targets->Build Phases->Link Binary) के तहत अपनी प्रोजेक्ट सेटिंग्स में CoreLocation.framework जोड़ने के लिए मत भूलना। // mobileorchard:

+0

लेकिन ये विधि अब वर्तमान स्थान को वापस नहीं कर रही है। मैं एक्सकोड 6 का उपयोग कर रहा हूं। लेकिन एक्सकोड 5 का उपयोग करते समय यह ठीक काम किया गया था। – SARATH

15

CLLocationManager के साथ आपको तुरंत स्थान जानकारी प्राप्त करने की आवश्यकता नहीं है। स्थान जानकारी प्राप्त करने वाले जीपीएस और अन्य डिवाइस प्रारंभ नहीं किए जा सकते हैं। उन्हें कोई जानकारी होने से पहले कुछ समय लग सकता है। इसके बजाय आपको एक प्रतिनिधि ऑब्जेक्ट बनाने की आवश्यकता है जो locationManager:didUpdateToLocation:fromLocation: पर प्रतिक्रिया दे और फिर इसे स्थान प्रबंधक के प्रतिनिधि के रूप में सेट करें।

here

+2

यह लेख http में साथ वर्तमान स्थान दिखा सकते हैं।कॉम/हैलो-ए-कोरलोकेशन-ट्यूटोरियल/(संबंधित स्टैक ओवरफ्लो प्रश्न से लिया गया) दिखाता है कि क्या करना है। – ThomasW

-2

यहाँ आप एनोटेशन विवरण

ViewController.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 


//My 
#import <MapKit/MapKit.h> 
#import <MessageUI/MFMailComposeViewController.h> 

@interface ViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate,MFMailComposeViewControllerDelegate> 
{ 
    IBOutlet UILabel *lblLatitiude; 
    IBOutlet UILabel *lblLongitude; 
    IBOutlet UILabel *lblAdress; 
} 
//My 
@property (nonatomic, strong) IBOutlet MKMapView *mapView; 


-(IBAction)getMyLocation:(id)sender; 

@end 

में

ViewController.m

#import "ViewController.h" 


@interface ViewController() 

@end 

@implementation ViewController{ 
    CLLocationManager *locationManager; 
    CLGeocoder *geocoder; 
    CLPlacemark *placemark; 
} 

@synthesize mapView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    locationManager = [[CLLocationManager alloc] init]; 
    geocoder = [[CLGeocoder alloc] init]; 

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    NSMutableDictionary *defaultsDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"[email protected]", @"fromEmail", 
               @"[email protected]", @"toEmail", 
               @"smtp.gmail.com", @"relayHost", 
               @"[email protected]", @"login", 
               @"[email protected]", @"pass", 
               [NSNumber numberWithBool:YES], @"requiresAuth", 
               [NSNumber numberWithBool:YES], @"wantsSecure", nil]; 

    [userDefaults registerDefaults:defaultsDictionary]; 


    self.mapView.delegate=self; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Custom Methods 


-(IBAction)getMyLocation:(id)sender{ 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 
} 


#pragma mark - CLLocationManagerDelegate 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 
    UIAlertView *errorAlert = [[UIAlertView alloc] 
           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 
     lblLongitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
     lblLatitiude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    } 

    // Stop Location Manager 
    [locationManager stopUpdatingLocation]; 

    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 

      lblAdress.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", 
           placemark.subThoroughfare, placemark.thoroughfare, 
           placemark.postalCode, placemark.locality, 
           placemark.administrativeArea, 
           placemark.country]; 



      MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, 800, 800); 
      [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; 

      // Add an annotation 
      MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
      point.coordinate = currentLocation.coordinate; 
      point.title = @"Where am I?"; 
      point.subtitle = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", 
           placemark.subThoroughfare, placemark.thoroughfare, 
           placemark.postalCode, placemark.locality, 
           placemark.administrativeArea, 
           placemark.country]; 


      [self.mapView addAnnotation:point]; 


     } else { 
      NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 


} 

//My 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"]; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotationView; 
} 

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 

    [self getSignScreenShot]; 

    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 
    [controller setSubject:@"My Subject"]; 
    [controller setMessageBody:@"Hello there." isHTML:NO]; 
    if (controller) [self presentModalViewController:controller animated:YES]; 
} 

- (void)mailComposeController:(MFMailComposeViewController*)controller 
      didFinishWithResult:(MFMailComposeResult)result 
         error:(NSError*)error; 
{ 
    if (result == MFMailComposeResultSent) { 
     NSLog(@"It's away!"); 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 

//----------------------------------------------------------------------------------- 
//This methos is to take screenshot of map 
//----------------------------------------------------------------------------------- 
-(UIImage *)getSignScreenShot 
{ 
    CGRect rect = CGRectMake(self.mapView.frame.origin.x,self.mapView.frame.origin.y-50,self.mapView.frame.size.width+60,self.mapView.frame.size.height+15); 
    UIGraphicsBeginImageContextWithOptions(self.mapView.frame.size, NO, 1.0); 
    [self.mapView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect); 
    UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 

    return newImage; 
}