Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: The SDK is constantly using the location when the user permits the App to use location 'Always' #3096

Open
Stophface opened this issue Oct 8, 2023 · 1 comment

Comments

@Stophface
Copy link

Stophface commented Oct 8, 2023

Mapbox Implementation

Mapbox

Mapbox Version

10.7.0

Platform

iOS

@rnmapbox/maps version

rnmapbox/maps#main

Standalone component to reproduce

import React from 'react';
import PropTypes from 'prop-types';
import {MapView, UserLocation} from '@rnmapbox/maps';
import {useIsFocused} from '@react-navigation/native';
import {connect} from 'react-redux';

const APP_STATE = {
  active: 'active',
  inactive: 'inactive',
  background: 'background',
};

class Mapp extends React.Component {
  _contidionallyRenderUserLocation = () => {
    const {isFocused, appState} = this.props;
    if (isFocused && APP_STATE.active === appState)
      return (
        <UserLocation
          showsUserHeadingIndicator={true}
          visible={true}
          renderMode="native"
        />
      );
  };

  render() {
    return <MapView>{this._contidionallyRenderUserLocation()}</MapView>;
  }
}

const EnhancedMapp = props => {
  const isFocused = useIsFocused();
  return <Mapp {...props} isFocused={isFocused} />;
};

const mapStateToProps = state => {
  const {appSettings} = state;
  return {
    appState: appSettings.appState,
  };
};

Mapp.propTypes = {
    appState: PropTypes.string,
    isFocused: PropTypes.boolean
}

export default connect(mapStateToProps, null)(EnhancedMapp);

appSettings is an event listener, basically this

import {AppState} from 'react-native';

...
const appStateSubscription = AppState.addEventListener( 
      'change',
      nextAppState => {
        if (
          nextAppState === APP_STATE.active ||
          nextAppState === APP_STATE.background
        ) {
          store.dispatch(setAppState(nextAppState));
        }

        appState.current = nextAppState;
      })
...

Observed behavior and steps to reproduce

When the user gives the app Location Permission - Always the Maps SDK uses the location always, when the App is in the foreground (opened) or in the background (closed, but not terminated).

Expected behavior

I expect to be able to set a property that tells the SDK 'Use the location now' or 'Do not use the location when the App is in the background', 'Do not access the location, eventhough you have permission always' ... or something similiar.

Notes / preliminary analysis

This is a thing arising with React-Navigation and Tab Navigator. When using a Tab Navigator, React Naigation does not unmount a component once its mounted inside a Tab Navigator. I though that <UserLocation /> is the component responsible for using the location all the time, so I tried to conditionally render it with isFocused = false and appState === 'active'. isFocused becomes true when the Tab Navigator focussed on the map. appState === 'active becomes true when the app is in the foreground. However, even with the precautions on the <UserLocation/>, the SDK always uses the location, doesnt really matter if the app is in the foreground or in the background, if the corresponding Tab in the Tab Navigator is focussed or not. So I assume the <MapView> is responsible for using the location always?

Additional links and references

No response

@Invictus-Munish
Copy link

Did you find any solution ? I am facing the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants