Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Using animateCamera in functional components in react native

import {useState, useRef} from 'react'
import ... //same as before

const PlayScreen = (props) => {
    const markerLatitude=32.5983
    const markerLongitude=44.0175
    //changed from useState to useRef
    const mapRef = useRef (null);
    const [myMarker, setMyMarker] = useState(null);
    const [coordinate, setCoordinate] = useState(new AnimatedRegion({
        latitude: markerLatitude,
        longitude: markerLongitude,
        latitudeDelta: 0.012,
        longitudeDelta: 0.012,
    }));

    function animateMarkerAndCamera() {

        let newCoordinate = {
            latitude: 32.601,
            longitude: 44.0172,
            latitudeDelta: 0.012,
            longitudeDelta: 0.012,
        };
        //camera will position itself to these coordinates.
        const newCamera= {
            center: {
                latitude: 32.601,
                longitude: 44.0172,
            },
            pitch: 0,
            heading: 0,
            //zoom: 17  --Use it when required
        }
        
        
        if (myMarker) {
            myMarker.animateMarkerToCoordinate(newCoordinate, 4000);
            //camera type, `newCamera`, used inside animateCamera
            mapRef.current.animateCamera(newCamera, {duration: 4000})
        }
        

    }

    return (
        <View style={styles.container}>
            <MapView
                ref={ mapRef } //There is also change here
                style={styles.map}
                initialRegion={{
                    latitude: 32.5983,
                    longitude: 44.0175,
                    latitudeDelta: 0.012,
                    longitudeDelta: 0.012,
                }}
                //These are newly added
                pitchEnabled ={false}
                zoomEnabled ={false}
            >
                <MarkerAnimated
                    ref={marker => {
                        setMyMarker(marker);
                    }}
                    {/*any kind of image can be replaced here */}
                    image={require('../../../Assets/Images/curlingStone.png')}
                    coordinate={coordinate}
                    
                />

            </MapView>
            <View style={styles.buttonContainer}>
                <TouchableOpacity
                    onPress={() => animateMarkerAndCamera()}
                    style={[styles.bubble, styles.button]}
                >
                    <Text>Animate</Text>
                </TouchableOpacity>
            </View>
        </View>
    );

}
export default PlayScreen

const styles = StyleSheet.create({ ... //same as before
Comment

PREVIOUS NEXT
Code Example
Javascript :: Delete a field from Firebase Firestore where the field/key has a period/punctuation (".") - modular v9 JavaScript SDK 
Javascript :: check if Popups and Redirects are allowed 
Javascript :: sfc setup vue 3 mounted method - lifecycle methods in sfc 
Javascript :: adding text to ant media stream 
Javascript :: How to use search/filter for HTML Divs generated from JSON data using JavaScript 
Javascript :: supertest npm send headers node js 
Javascript :: js generate pnh 
Javascript :: track call recording in facebook using elements 
Javascript :: sending api with limited fields in express 
Javascript :: Streaming search queries with Node.js and Socket.io (streaming to a given socket 
Javascript :: ant design rtl 
Javascript :: Triggering An Event Programmatically With JavaScript 
Javascript :: mongo db get child result with array of parent ids 
Javascript :: Odoo Javascript Modules 
Javascript :: queryselector undefined not working in react js 
Javascript :: phaser move towards object 
Javascript :: Constructor for blockchain 
Javascript :: barcode javascript library 
Javascript :: pass a react component as a prop from another component 
Javascript :: Create Built-in AbortController Object 
Javascript :: detect sound chrome extension every 1 second 
Javascript :: python code to javascript converter 
Javascript :: how to find default or the first server discord.js 
Javascript :: stringToCapital.js 
Javascript :: javascript enter key 
Javascript :: name of javascript virtual machine for apple 
Javascript :: counter example using classes react without jsx 
Javascript :: jquery try catch 
Javascript :: convert milliseconds to dd/mm/yyyy javascript 
Javascript :: dynamic styles in react native 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =