2016-05-20 9 views
-1

Ich habe versucht, viel dafür zu suchen, aber ich bekam keine genaue Antwort, was ich will.Senden des aktuellen Standortes an den Server im Hintergrund sowie in der laufenden App mit AFNetworking 3

meine Frage ist

Kann ich Benutzer-aktuelle Standort-Server senden, wenn App als auch im Hintergrund ausgeführt wird.

Zum Beispiel: In meiner App möchte ich den aktuellen Standort des Benutzers in jeder 2min speichern und dann seinen aktuellen Standort an server.as sowie wenn Benutzer 50 Meter innerhalb von weniger als 2min dann auch verschieben. Wie kann ich das tun? Kann ich Standort-Updates sowohl im Hintergrund als auch im Vordergrund an den Server senden? Wenn ja, wie?

Ich habe versucht, wie folgt:

self.locations = [[NSMutableArray alloc] init]; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.distanceFilter = 10; 

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
self.locationManager.delegate = self; 
[self.locationManager requestAlwaysAuthorization]; 

[self.locationManager startUpdatingLocation]; 

...

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
{ 



// Add another annotation to the map. 
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; 
annotation.coordinate = newLocation.coordinate; 
[self.map addAnnotation:annotation]; 

// Also add to our map so we can remove old values later 
[self.locations addObject:annotation]; 

// Remove values if the array is too big 
while (self.locations.count > 1) 
{ 
    annotation = [self.locations objectAtIndex:0]; 
    [self.locations removeObjectAtIndex:0]; 

    // Also remove from the map 
    [self.map removeAnnotation:annotation]; 
} 


if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) 
{ 
    // determine the region the points span so we can update our map's zoom. 
    double maxLat = -91; 
    double minLat = 91; 
    double maxLon = -181; 
    double minLon = 181; 

    for (MKPointAnnotation *annotation in self.locations) 
    { 
     CLLocationCoordinate2D coordinate = annotation.coordinate; 

     if (coordinate.latitude > maxLat) 
      maxLat = coordinate.latitude; 
     if (coordinate.latitude < minLat) 
      minLat = coordinate.latitude; 

     if (coordinate.longitude > maxLon) 
      maxLon = coordinate.longitude; 
     if (coordinate.longitude < minLon) 
      minLon = coordinate.longitude; 
    } 

    MKCoordinateRegion region; 
    region.span.latitudeDelta = (maxLat + 90) - (minLat + 90); 
    region.span.longitudeDelta = (maxLon + 180) - (minLon + 180); 

    _latitude1=[NSString stringWithFormat:@"Latitude:%f",newLocation.coordinate.latitude]; 
    _longitude1=[NSString stringWithFormat:@"Longitude:%f",newLocation.coordinate.longitude]; 


    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    appDelegate.fetchtokenString1 = [NSString stringWithString:_latitude1]; 
    appDelegate.fetchedriveridString1 = [NSString stringWithString:_longitude1]; 





    NSLog(@"%@",_latitude1); 
    NSLog(@"%@",_longitude1); 


    // the center point is the average of the max and mins 
    region.center.latitude = minLat + region.span.latitudeDelta/2; 
    region.center.longitude = minLon + region.span.longitudeDelta/2; 

    // Set the region of the map. 
    [self.map setRegion:region animated:YES]; 
} 
else 
{ 
    NSLog(@"App is backgrounded. New location is %@", newLocation); 
} 

Vielen Dank im Voraus ..

+0

können Sie Ihren Code .. – Santo

+0

Ihr Problem ist nicht auf gestern –

+0

Dies ist ein anderes Problem, das ich konfrontiert bin .. @ Anbu.Karthik ... Ich möchte aktuelle Position in FOREGROUND sowie Hintergrund in jedem senden 2MIN sowie (wenn der Benutzer 50 Meter innerhalb von 2 Min. Bewegt, dann auch) ...... deine Antwort von gestern wirklich am besten ... danke dafür ... aber das in der neuen Ausgabe, mit der ich konfrontiert bin ... bitte helft Ich –

Antwort

0

überprüfen dies bei Ihrer App im Vordergrund läuft.

How can I get current location from user in iOS

prüft diese Lage zu senden, wenn die App im Hintergrund ist.

Sending Latitude and Longitude to Server when app is in background

nach Breitengrad und Längengrad bekommen, senden Sie durch AFNetworking Framework.

ODER

Wenn Sie von Grund auf neu benötigen, wenn Sie nicht wissen, wie dann erhalten Lage die folgenden Schritte folgen Breiten- und Längengrad zu erhalten.

Fügen Sie diese unter zwei String-Typen in Ihrem info.plist

enter image description here

Dann Corelocation-Framework in Ihrem Projekt hinzufügen.

Import es zu Ihrem ViewController.h

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

@interface ViewController : UIViewController<CLLocationManagerDelegate> 
@property(nonatomic, retain) CLLocationManager *locationManager; 

@end 

tun Dann unten in Ihrem ViewController.m

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
#ifdef __IPHONE_8_0 
if(IS_OS_8_OR_LATER) { 
    // Use one or the other, not both. Depending on what you put in info.plist 
    [self.locationManager requestWhenInUseAuthorization]; 
    [self.locationManager requestAlwaysAuthorization]; 
} 
#endif 
[self.locationManager startUpdatingLocation]; 


} 

-(void)viewWillAppear:(BOOL)animated { 

NSLog(@"latitude: %f, longitude: %f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude); 
} 
- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

Log Breite und Länge zeigen.

+0

Ich bekomme aktuelle Position im Vordergrund sowie Hintergrund bro ..... aber lesen Sie meine Frage richtig ..Ich möchte den Standort senden, wenn sich der Standort ändert .... sowie wenn sich der Standort seit den letzten 2 Minuten nicht ändert. dann auch Standort an den Server senden ..... @ Santo –

+0

@SurajSukale Sie sagten, Sie haben nicht einmal richtig Lage, so habe ich über eins, um lat lange – Santo

+0

Ich möchte den aktuellen Standort des Benutzers in jeder 2min holen und dann senden Sie seinen aktuellen Standort zu server.as sowie wenn Benutzer 50 Meter in weniger als 2min dann auch bewegen. Darf ich das machen ? ..dies ist mein aktuelles Problem .... bitte hilf mir dieses Problem zu lösen Bruder .... @ Santo –