maps icon indicating copy to clipboard operation
maps copied to clipboard

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

Open Stophface opened this issue 1 year ago • 1 comments

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

Stophface avatar Oct 08 '23 11:10 Stophface

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

Invictus-Munish avatar Dec 27 '23 12:12 Invictus-Munish