ios - CLLocationManager location monitoring not working properly -


i have app notify user every time approaches 1 of client's stores. there more 20 stores, have function takes user's location , finds 20 nearest stores him , start monitoring location of these stores, every time user moves, app finds 20 nearest stores again, removes previous stores monitoring , start monitoring new ones.

for reason, doesn't work, i'll happy if 1 of (or more :)) me find problem, thanks!!

mycode (scroll see full code):

note: cllocationmanager created on appdelegate.m , it's delegate class (uiviewcontroller).

-(void)sortcloseststores {     [self.allstores sortusingcomparator:^nscomparisonresult(id  _nonnull obj1, id  _nonnull obj2) {         cllocation *location1=[[cllocation alloc] initwithlatitude:((store*)obj1).geopoint.latitude longitude:((store*)obj1).geopoint.longitude];         cllocation *location2=[[cllocation alloc] initwithlatitude:((store*)obj2).geopoint.latitude longitude:((store*)obj2).geopoint.longitude];      float dist1 =[location1 distancefromlocation:self.locationmanager.location];     float dist2 = [location2 distancefromlocation:self.locationmanager.location];     if (dist1 == dist2) {         return nsorderedsame;     }     else if (dist1 < dist2) {         return nsorderedascending;     }     else {         return nsordereddescending;     } }];  if (self.twentycloseststores==nil) {     self.twentycloseststores=[nsmutablearray array]; }  if (self.previoustwentystores==nil) {     self.previoustwentystores=[nsmutablearray array]; } self.previoustwentystores=self.twentycloseststores; self.twentycloseststores=[nsmutablearray array];  (int = 0; < 20; i++) {     [self.twentycloseststores addobject:[self.allstores objectatindex:i]];     } }  -(void)startmonitoringcloseststores {     if (![cllocationmanager ismonitoringavailableforclass:[clcircularregion class]]) {         nslog(@"monitoring not available clcircularregion class");     }      (store *currentstore in self.twentycloseststores) {         clcircularregion *region=[currentstore createcircularregion];          [self.locationmanager startmonitoringforregion:region];     } } -(void)stopmonitoringstores {     (store *currentstore in self.previoustwentystores) {         clcircularregion *region=[currentstore createcircularregion];         [self.locationmanager stopmonitoringforregion:region];     } }  -(void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray<cllocation *> *)locations {     if (self.allstores!=nil) {         [self sortcloseststores];         [self stopmonitoringstores];         [self startmonitoringcloseststores];     } }   -(void)locationmanager:(cllocationmanager *)manager didenterregion:(clregion *)region {     nslog(@"entered"); //not called when user enters 1 of regions. } 

can please me? thanks!

i'm pretty new @ corelocation myself think not idea call stopmonitoringforregions , startmonitoringforregions in didupdatelocations.

since you're monitoring regions, didenterregion delegate interested in. give 'hey, arrived @ x store' event, , in there want call code have in didupdatelocations.

you want setup corelocation in appdelegate, might have (sorry being swift, that's i'm working in right now):

locationmanager.delegate = self // auths: locationmanager.requestalwaysauthorization() locationmanager.requestwheninuseauthorization() // config: locationmanager.distancefilter = kcldistancefilternone locationmanager.desiredaccuracy = kcllocationaccuracybest locationmanager.pauseslocationupdatesautomatically = true locationmanager.allowsbackgroundlocationupdates = true locationmanager.activitytype = clactivitytype.automotivenavigation  // start: locationmanager.startmonitoringsignificantlocationchanges() 

then have code:

if (self.allstores!=nil) {     [self sortcloseststores];     [self stopmonitoringstores];     [self startmonitoringcloseststores]; } 

note: don't think matters if call startmonitoringsignificantlocationchanges before or after adding monitored regions, haven't gotten quite far in code yet.

didupdatelocations more when want track location e.g. tracking bicycle ride or jogging session.

additional explanation: ok, think understand issue now. there 2 aspects want accomplish:

  • being notified when user enters store's region
  • dynamically recalculating nearest n stores device moves

my previous answer geared towards first issue. regarding second issue, dynamically recalulating nearest n, code in didupdatelocations not called unless tell location manager startupdatinglocation. off top of head:

// configure location manager in appdelegate: // need experiment these 2 properties, // don't want use kcllocationaccuracybestfornavigation.  // maybe kcllocationaccuracykilometer sufficient. locmgr.distancefilter = n locmgr.desiredaccuracy = m locmgr.pauseslocationupdatesautomatically = true locmgr.startupdatinglocation()    // delegate receive events due locmgr.startupdatinglocation locationmanager:didupdatelocation {     // unless have specific need it, refactor     // don't need self.previoustwentystores:     [self stopmonitoringstores];     [self sortcloseststores];     [self startmonitoringcloseststores]; }   locationmanager:didenterregion {     // in region surrounding 1 of stores. } 

alternately, consider setting timer , waking app every n seconds or minutes recalculate nearest stores.

as understand various aspects of corelocation

startupdatinglocation -> didupdatelocations -> stopupdatinglocation

is (in 1 sense) entirely separate from:

startmonitoringforregion -> didenterregion | didexitregion -> stopmonitoringforregion

additionally, startupdatinglocation never called didupdatelocation never called.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -