AGSLocationDisplay refresh

3941
2
Jump to solution
05-20-2015 09:10 PM
QKunZhu
New Contributor II

What I am trying to do is, when you press a button, the app auto pan and zoom to current location. Then I can pan the map to other place, and when I press that button again, it will still bring me to my current location (auto pan and zoom).

My problem is, when I first press the button, it works as i expected. Then I move to other place on the map, and when i press the button again, it does not auto pan and zoom to my current location.

Code is below:

- (void)viewDidLoad {
    [super viewDidLoad];

     // add map layers
     ... ...

     
     self.mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanModeCompassNavigation;
     self.mapView.locationDisplay.navigationPointHeightFactor = 0.5;
     [self.mapView.locationDisplay addObserver:self forKeyPath:@"location" options:NSKeyValueObservingOptionNew context:NULL];
} 

- (IBAction)showCurrentLocation:(id)sender {

    [self.mapView.locationDisplay startDataSource];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
   
    if ([keyPath isEqual:@"location"]) {
       
        // creating graphics on current location
          ... ...

        [self.mapView.locationDisplay stopDataSource];
    }
}
0 Kudos
1 Solution

Accepted Solutions
MengyiGuo
Occasional Contributor

In your observeValueForKeyPath method, you should not stopDataSource. That will stop the location and you will not be able to display it. I modified your code as follows:

-(void) mapViewDidLoad:(AGSMapView*)mapView {

   

    // Enable location display on the map

    [self.mapView.locationDisplay startDataSource];

    self.mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanModeDefault;

   

    [self.mapView zoomToScale:500.0 animated:true];

  

}

- (void)viewDidUnload {

    //Stop the GPS, undo the map rotation (if any)

    if(self.mapView.locationDisplay.dataSourceStarted){

        [self.mapView.locationDisplay stopDataSource];

        self.mapView.rotationAngle = 0;

     

    }

    self.mapView = nil;

}

- (IBAction)showCurrentLocation:(id)sender {

   

    if(!self.mapView.locationDisplay.dataSourceStarted)

        [self.mapView.locationDisplay startDataSource];

   

    self.mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanModeDefault;

}

View solution in original post

0 Kudos
2 Replies
MengyiGuo
Occasional Contributor

In your observeValueForKeyPath method, you should not stopDataSource. That will stop the location and you will not be able to display it. I modified your code as follows:

-(void) mapViewDidLoad:(AGSMapView*)mapView {

   

    // Enable location display on the map

    [self.mapView.locationDisplay startDataSource];

    self.mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanModeDefault;

   

    [self.mapView zoomToScale:500.0 animated:true];

  

}

- (void)viewDidUnload {

    //Stop the GPS, undo the map rotation (if any)

    if(self.mapView.locationDisplay.dataSourceStarted){

        [self.mapView.locationDisplay stopDataSource];

        self.mapView.rotationAngle = 0;

     

    }

    self.mapView = nil;

}

- (IBAction)showCurrentLocation:(id)sender {

   

    if(!self.mapView.locationDisplay.dataSourceStarted)

        [self.mapView.locationDisplay startDataSource];

   

    self.mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanModeDefault;

}

0 Kudos
QKunZhu
New Contributor II

Thank you so much! It works.

0 Kudos