Преглед изворни кода

Apex & drivers
将定位拆分为当前位置和最后位置
服务器下发位置请求改为回报最后已知位置
增加上报最后位置时间
修改定位参数,优化电量消耗

Ray Zhang пре 7 година
родитељ
комит
db02ac67f7

+ 70 - 14
Redant Drivers/Apex And Drivers/AppDelegate.m

@@ -57,9 +57,13 @@
         [self.uploadManager saveTasks];
     }
 }
-
-- (void)startLocation {
-    
+//- (void)requestLocation
+//{
+//    [self initLocationManager];
+//    [self.locationManager requestLocation];
+//}
+-(void)initLocationManager
+{
     if (self.locationManager) {
         return;
     }
@@ -67,9 +71,14 @@
     self.locationManager.delegate = self;
     self.locationManager.allowsBackgroundLocationUpdates = YES;
     self.locationManager.pausesLocationUpdatesAutomatically = NO; // 是否允许系统自动暂停定位
-    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//设置定位精度
+    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;//设置定位精度
+    
     self.locationManager.distanceFilter = 10; //不需要移动都可以刷新,不然不移动就不会执行定位,不定位的话,那么后台进程也就挂起了
     [self.locationManager requestAlwaysAuthorization];
+}
+- (void)startLocation {
+    [self initLocationManager];
+
     [self.locationManager startUpdatingLocation];
 }
 
@@ -78,7 +87,7 @@
         return;
     }
     [self.locationManager stopUpdatingLocation];
-    self.locationManager = nil;
+//    self.locationManager = nil;
 }
 
 - (void)receiveLogoutNotification:(NSNotification *)notification {
@@ -280,11 +289,11 @@
             NSString *orderID = [aps objectForKey:@"order-id"];
             if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAllow) {
                 
-                [self reportLocation:[RASingleton sharedInstance].currentLocation forOrder:orderID];
+                [self reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID];
                 
-            } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAlways) {
+            } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeAlwaysAsk) {
                 
-                [self reportLocationWithOrder:orderID];
+                [self askForReportLastLocation:orderID];
             } else if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeReject) {
                 
                 [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Driver %@ rejected to report location",RASingleton.sharedInstance.user] forOrder:orderID];
@@ -357,7 +366,19 @@
         [RADataProvider reportLocationWithUserReason:reason forOrder:orderID];
     });
 }
-
+- (void)reportLastLocation:(CLLocation *)location forOrder:(NSString *)orderID {
+    
+    NSString *latLon = nil;
+    if (location) {
+        latLon = [NSString stringWithFormat:@"%f,%f",location.coordinate.latitude,location.coordinate.longitude];
+        
+    } else {
+        latLon = @"-999,-999";
+    }
+    dispatch_async(dispatch_get_global_queue(0, 0), ^{
+        [RADataProvider reportLastLocation:latLon forOrderID:orderID];
+    });
+}
 - (void)reportLocation:(CLLocation *)location forOrder:(NSString *)orderID {
     
     NSString *latLon = nil;
@@ -371,7 +392,36 @@
         [RADataProvider reportCurrentLocation:latLon forOrderID:orderID];
     });
 }
-
+- (void)askForReportLastLocation:(NSString *)orderID {
+    
+    UIViewController *topVC = self.window.rootViewController;
+    while (topVC.presentedViewController) {
+        topVC = topVC.presentedViewController;
+    }
+    
+    if (topVC) {
+        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:[NSString stringWithFormat:@"Report Location For Order:%@",orderID] preferredStyle:UIAlertControllerStyleAlert];
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            
+            [self rejectReportLocationWithReason:[NSString stringWithFormat:@"Driver %@ cancel to report location",RASingleton.sharedInstance.user] forOrder:orderID];
+            
+        }];
+        
+        __weak typeof(self) weakSelf = self;
+        UIAlertAction *reportAction = [UIAlertAction actionWithTitle:@"Report" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
+            
+            [weakSelf reportLastLocation:[RASingleton sharedInstance].lastLocation forOrder:orderID];
+            
+        }];
+        
+        [alertVC addAction:cancelAction];
+        [alertVC addAction:reportAction];
+        
+        [topVC presentViewController:alertVC animated:YES completion:nil];
+        
+        [self sendLocalNotification:@"Report Location Notification" message:[NSString stringWithFormat:@"The Apex ask your location for order:%@",orderID]];
+    }
+}
 - (void)reportLocationWithOrder:(NSString *)orderID {
     
     UIViewController *topVC = self.window.rootViewController;
@@ -408,18 +458,24 @@
 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
     if (locations.count) {
         [RASingleton sharedInstance].currentLocation = [locations lastObject];
-        
+        [RASingleton sharedInstance].lastLocation = [locations lastObject];
+        [RASingleton sharedInstance].lastLocationDateTime=[RAUtils current_date];
         NSLog(@"location: %f, %f",[RASingleton sharedInstance].currentLocation.coordinate.latitude,[RASingleton sharedInstance].currentLocation.coordinate.longitude);
         
         // 省电,停止不能超过三分钟
 //        [self performSelector:@selector(stopLocation) withObject:nil afterDelay:10]; // 获取到位置10s后关闭位置服务
 //        [self performSelector:@selector(startLocation) withObject:nil afterDelay:120]; // 获取到位置120s后重新打开位置服务
-        CLLocationDistance distance = 500;
-        NSTimeInterval time = 60;
+        //后台延迟定位3km,5分钟。
+        CLLocationDistance distance = 3000;
+        NSTimeInterval time = 60*5;
         [manager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];
     }
 }
-
+- (void)locationManager:(CLLocationManager *)manager
+       didFailWithError:(NSError *)error
+{
+    [RASingleton sharedInstance].currentLocation = nil;
+}
 - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
     
     if (status == kCLAuthorizationStatusDenied) {

+ 1 - 1
Redant Drivers/Apex And Drivers/Login/LoginViewController.m

@@ -88,7 +88,7 @@
         }];
         
         UIAlertAction *alwaysAction = [UIAlertAction actionWithTitle:@"Always ask" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-            RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAlways;
+            RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAlwaysAsk;
         }];
         
         UIAlertAction *allowAction = [UIAlertAction actionWithTitle:@"Allow" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

+ 1 - 0
Redant Drivers/Apex And Drivers/RADataProvider.h

@@ -29,6 +29,7 @@
 + (NSDictionary *)uploadFile:(NSString *)filePath parameters:(NSMutableDictionary *)params;///<同步上传文件
 
 + (NSDictionary *)reportCurrentLocation:(NSString *)location forOrderID:(NSString *)orderID;
++ (NSDictionary *)reportLastLocation:(NSString *)location forOrderID:(NSString *)orderID;
 
 + (NSDictionary *)reportLocationWithUserReason:(NSString *)option forOrder:(NSString *)orderID;
 

+ 20 - 1
Redant Drivers/Apex And Drivers/RADataProvider.m

@@ -262,7 +262,26 @@
 + (NSDictionary *)uploadFile:(NSString *)filePath parameters:(NSMutableDictionary *)params {
     return [self upload:URL_UPLOAD parameters:params file:filePath];
 }
-
++ (NSDictionary *)reportLastLocation:(NSString *)location forOrderID:(NSString *)orderID {
+    
+    NSMutableDictionary *params = [NSMutableDictionary dictionary];
+    if (location) {
+        [params setObject:location forKey:@"location"];
+    }
+    if (orderID) {
+        [params setObject:orderID forKey:@"orderID"];
+    }
+    NSString * lastLocationDateTime = RASingleton.sharedInstance.lastLocationDateTime;
+    if(lastLocationDateTime.length>0)
+    {
+        [params setObject:lastLocationDateTime forKey:@"lastLocationDateTime"];
+    }
+    [params setObject:@(0) forKey:@"userOption"]; // 0 表示同意发送位置
+    
+    NSData* json=[self get_json:URL_REPORT_LOCATION parameters:params  file:nil];
+    
+    return [self handleJsonData:json];
+}
 + (NSDictionary *)reportCurrentLocation:(NSString *)location forOrderID:(NSString *)orderID {
     
     NSMutableDictionary *params = [NSMutableDictionary dictionary];

+ 3 - 2
Redant Drivers/Apex And Drivers/RASingleton.h

@@ -11,7 +11,7 @@
 typedef enum {
     RABackgroundReportTypeNone = 0, ///<没有设置
     RABackgroundReportTypeReject = 1, ///< 拒绝
-    RABackgroundReportTypeAlways = 2, ///< 每次询问
+    RABackgroundReportTypeAlwaysAsk = 2, ///< 每次询问
     RABackgroundReportTypeAllow = 3 ///< 同意
     
 } RABackgroundReportType;
@@ -24,7 +24,8 @@ typedef enum {
 @property (nonatomic,copy,readonly) NSString *user;
 @property (nonatomic,copy,readonly) NSString *password;
 @property (nonatomic,strong) CLLocation *currentLocation;
-
+@property (nonatomic,strong) CLLocation *lastLocation;
+@property (nonatomic,strong) NSString *lastLocationDateTime;
 @property (nonatomic,copy,readonly) NSString *secretKey;
 @property (nonatomic,copy,readonly) NSString *encryptUser;
 @property (nonatomic,copy,readonly) NSString *encryptPassword;

BIN
Redant Drivers/Redant Drivers.xcworkspace/xcuserdata/ray.xcuserdatad/UserInterfaceState.xcuserstate