I have a compass that I created in react native. It has a couple of issues and I don't know how to best approach them.
Firstly, the arrow in the middle is hardcoded with top and left which seems to make it not responsive. I tried setting parent to relative and arrow to absolute.
Secondly, the angle doesn't stay consistent to the heading. Realistically, heading will fluctuate as the user moves the phone. Going anywhere from 0 to 360 in value.
The issue is that the angle needs to follow the heading. At 0... they seem to be fine but if the heading is changed to 200... the angle should be facing towards 0 not 200.
I tried to change the transform for arrow to this:
270 - heading - angle
But seems to be off sometimes, regardless
How can i go about solving this?
export const App: React.FC = () => {
const [angle, setAngle] = useState(0);
const [heading, setHeading] = useState(200);
const updateHeading = (headingObject) => {
const roundedHeading = Math.round(headingObject.trueHeading);
setHeading(roundedHeading);
};
return (
<View style={{ backgroundColor: 'gray' }}>
<View style={styles.row}>
<Text style={styles.angle}>Angle is: {angle}°</Text>
<View style={styles.arrowContainer}>
<Image
source={require('./assets/arrow.png')}
style={{
width: width / 10,
height: width / 10,
left: '47%',
top: 160,
resizeMode: 'contain',
transform: [{ rotate: angle + 'deg' }],
}}
/>
</View>
</View>
<View
style={styles.compassWrapper}>
<Image
source={require('./assets/compass_bg.png')}
style={{
height: width - 80,
width: width,
resizeMode: 'contain',
transform: [{ rotate: 270 - heading + 'deg' }],
}}
/>
</View>
</View>
);
};
export default App;
Demo:


