mapbox-gl-native icon indicating copy to clipboard operation
mapbox-gl-native copied to clipboard

MapBox didUpdateUserLocation called twice for every location update

Open standers67 opened this issue 8 years ago • 6 comments

We use MapBox in one of our recent iOS 11 projects. In the project we have a phenomenon that cause big problems. For some reason the MapBox didUpdateUserLocation delegate method is called twice for every location update.

To illustrate the issue, we added this code which simply increase a class variable

#pragma mark -
#pragma mark MGLMapViewDelegate Methods
- (void)mapView:(MGLMapView *)mapView didUpdateUserLocation:(nullable 
MGLUserLocation *)userLocation
{
    NSLog(@"updatecounter = %i",updatecounter);
    updatecounter++;
}

The following sample output is generated:

2018-02-22 22:50:14.210199+0100 didUpdateUserLocation:] [Line 1830] updatecounter = 3
2018-02-22 22:50:14.210511+0100 didUpdateUserLocation:] [Line 1830] updatecounter = 3
2018-02-22 22:50:15.184880+0100 didUpdateUserLocation:] [Line 1830] updatecounter = 4
2018-02-22 22:50:15.187484+0100 didUpdateUserLocation:] [Line 1830] updatecounter = 4
2018-02-22 22:50:16.186877+0100 didUpdateUserLocation:] [Line 1830] updatecounter = 5
2018-02-22 22:50:16.189279+0100 didUpdateUserLocation:] [Line 1830] updatecounter = 5
....

If we remove the delegate connection to the viewController, the calls stop. Can anyone think of a reason what could cause MapBox to call the delegate twice for every update?

Any help would be appreciated

Thanks, Stefan

standers67 avatar Feb 23 '18 07:02 standers67

Hello @standers67 — which version(s) of our SDK are you seeing this issue with?

friedbunny avatar Feb 23 '18 16:02 friedbunny

Hello, sorry I should have specified that of course - we are on version 3.7.5, which I think is current. We added another NSLog entry in the delegate, sometimes the second call show the variable before it is incremented which also very confusing.

This is the current code in the delegate:

- (void)mapView:(MGLMapView *)mapView didUpdateUserLocation:(nullable MGLUserLocation *)userLocation
{
    NSLog(@"mapView = %@",mapView);
    NSLog(@"updatecounter = %i",updatecounter);
    updatecounter++;
}

This is the current output:

2018-02-23 17:51:14.409608+0100  mapView:didUpdateUserLocation:] [Line 1830] mapView = <MGLMapView: 0x7f9fe1809a00; frame = (0 64; 375 603); 
2018-02-23 17:51:14.410017+0100  mapView:didUpdateUserLocation:] [Line 1832] updatecounter = 83
2018-02-23 17:51:14.411353+0100  mapView:didUpdateUserLocation:] [Line 1830] mapView = <MGLMapView: 0x7f9fe18b4000; frame = (0 64; 375 603); 
2018-02-23 17:51:14.411539+0100  mapView:didUpdateUserLocation:] [Line 1832] updatecounter = 82

2018-02-23 17:51:15.415364+0100  mapView:didUpdateUserLocation:] [Line 1830] mapView = <MGLMapView: 0x7f9fe1809a00; frame = (0 64; 375 603); 
2018-02-23 17:51:15.415689+0100  mapView:didUpdateUserLocation:] [Line 1832] updatecounter = 84
2018-02-23 17:51:15.417144+0100  mapView:didUpdateUserLocation:] [Line 1830] mapView = <MGLMapView: 0x7f9fe18b4000; frame = (0 64; 375 603); 
2018-02-23 17:51:15.417297+0100  mapView:didUpdateUserLocation:] [Line 1832] updatecounter = 83

We simply cannot understand this, it seems that there are two delegate calls for each location update, with only a very short delay between them.

Thanks a lot for assisting with this!

/ Stefan

standers67 avatar Feb 23 '18 17:02 standers67

We have one MapBox in the ViewController, one reference and one delegate. While looking at the output from the delegate calls, the reference to the mapView alternates between two addresses. Does this make sense?

standers67 avatar Feb 24 '18 06:02 standers67

@standers67 It looks like there are somehow two map views (which isn’t normal or expected). Can you have a look at how you’re initializing the map view and make sure it won’t double-initialize, you’re not accidentally creating/referencing a second map view (e.g., one from a storyboard and one programmatically), etc. — thanks!

friedbunny avatar Feb 26 '18 17:02 friedbunny

Thanks a lot for looking into this, you were right on the spot here. A logic error in the code created two instances of the MapBox component from the storyboard. Thanks for helping us out and pointing in the right direction - appreciated!

standers67 avatar Feb 27 '18 01:02 standers67

Great, glad to hear it. 🙇

friedbunny avatar Feb 27 '18 01:02 friedbunny