2016-04-08 20 views
1

iphone'daki GPS verilerinden Lat ve Long kullanıcılarını almaya çalışıyorum. Burada Kullanıcıların konumunu Cocoa-touch'ta edinin

benim kodudur:
// 
// SendLocView.m 
// AMR App 
// 
// Created by Fletcher on 07/04/2016. 
// Copyright © 2016 LaserCamel Programing. All rights reserved. 
// 

#import "SendLocView.h" 
@import CoreLocation; 

@interface SendLocView() 

@end 

@implementation SendLocView 



- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. 
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
     [self.locationManager requestWhenInUseAuthorization]; 
    } 
    [self.locationManager startUpdatingLocation]; 
} 

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

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 
- (IBAction)location_send:(id)sender { 
    [self.locationManager requestAlwaysAuthorization]; 
    NSString *Globalusername = @"1111"; 
    float latitude = locationManager.location.coordinate.latitude; 
    float longitude = locationManager.location.coordinate.longitude; 

    NSLog(@"Value of username is %@", Globalusername); 
    NSLog(@"Value of latitude is %f", latitude); 
    NSLog(@"Value of longitude is %f", longitude); 


    NSString *post =[[NSString alloc] initWithFormat:@"lat=%f&long=%f&username=%@",latitude,longitude,Globalusername]; 
    NSLog(@"PostData: %@",post); 

    NSURL *url=[NSURL URLWithString:@"https://www.webaddress.org.uk/mobile/jsontracking.php"]; 

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 




} 


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    NSLog(@"%@", [locations lastObject]); 
} 

- (void)requestAlwaysAuthorization 
{ 
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; 

    // If the status is denied or only granted for when in use, display an alert 
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) { 
     NSString *title; 
     title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled"; 
     NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings"; 

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title 
                  message:message 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"Settings", nil]; 
     [alertView show]; 
    } 
    // The user has not enabled any location services. Request background authorization. 
    else if (status == kCLAuthorizationStatusNotDetermined) { 
     [self.locationManager requestAlwaysAuthorization]; 
    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 1) { 
     // Send the user to the Settings for this app 
     NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; 
     [[UIApplication sharedApplication] openURL:settingsURL]; 
    } 
} 

@end 

NSLog Benim Akım çıkışı

(@, enlem "enlem Değer% f");

2016-04-08 21:54:29.170 AMR App[1394:467604] Value of latitude is 0.000000 

Ancak NSLog (@ "% @", [konumlar lastObject]); çıkış

2016-04-08 21:55:38.186 AMR App[1394:467604] <+51.50483880,-3.16954134> +/- 65.00m (speed -1.00 mps/course -1.00) @ 08/04/2016, 21:55:38 British Summer Time 

neden bu

+0

En 'olasılıkla locationManager.location' Bir konum henüz alınmadığını belirten sıfırdır. –

+1

Mantıklı olmayan birkaç şey var; "WhenInUseAuthorization" isteğinde bulunuyorsunuz, ancak daha sonra "her zaman Yetkilendirme" yi istiyorsunuz - Hangisini istiyorsunuz? Yetkilendirme durumunun değiştiğini bildirmek için temsilci geri aranmasını beklemeden "startUpdatingLocation" ı da çağırıyorsunuz. Son olarak, 'location' özelliklerine erişebilmek için' didUpdateLocations' içinde bir yer almak için beklemek zorundasınız. Mülkün '@ IBAction' yönteminde sıfır olmadığını kontrol etmelisiniz. – Paulw11

cevap

1

CLLocationManager belgeleri o size yapılabilecek ne olabileceğini hakkında herhangi bir varsayım yapamaz, yani uyumsuz konum bilgilerini giderir belirtiyor harika olurdu oluyor için herhangi bir yardımcı Herhangi bir zamanda.

This method returns immediately. Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager:didUpdateLocations: method.

Yani, -locationManager:didUpdateLocations: temsilci yöntemi denir dek herhangi bir geçerli konum bilgilerini olmasını beklememelisiniz: Değerler ilk günlük mesajında ​​nil neden

-startUpdatingLocation için AppleDoc tartışma açıklar. Günlük mesajınızdaki zaman damgalarına dayanarak, konumun çözülmek üzere yaklaşık 1 saniye sürdüğü açıktır; bu, aslında Apple mühendisliğinden belgelenmiş zaman aralığıyla uyumludur.

Not: docs da başarısızlık yöntemi uygulayan önermek ...

In addition to your delegate object implementing the locationManager:didUpdateLocations: method, it should also implement the locationManager:didFailWithError: method to respond to potential errors.