Skip to content

Instantly share code, notes, and snippets.

@khalid32
Created July 18, 2017 05:47
Show Gist options
  • Select an option

  • Save khalid32/ffd31feab2bb2195747356a683260a7b to your computer and use it in GitHub Desktop.

Select an option

Save khalid32/ffd31feab2bb2195747356a683260a7b to your computer and use it in GitHub Desktop.
this is an example of how to move MapView.Marker smoothly by using Animated API and Easing...
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Dimensions,
Animated,
Easing,
} from 'react-native';
import MapView, {Marker} from 'react-native-maps';
const {width, height} = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE_DELTA = 0.0024524938278069897, LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
const styles = StyleSheet.create({
flexbox:{
flex: 1,
},
adjustCenter:{
justifyContent: 'flex-end',
alignItems: 'center',
},
backdrop:{
backgroundColor: '#38b7ed',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
fontWeight: '700',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
map: {
...StyleSheet.absoluteFillObject,
},
});
class WorldMap extends Component {
constructor(props){
super(props);
this.state = {
timestamp: 0,
region: new MapView.AnimatedRegion({
latitude: 0,
longitude: 0,
latitudeDelta: 0,
longitudeDelta: 0,
}),
coordinate: new MapView.AnimatedRegion({
latitude: 23.772046,
longitude: 90.3600703
}),
accuracy: 0,
heading: 0,
speed: 0,
altitude: 0
};
}
watchId: ? number = null;
componentWillMount(){
navigator.geolocation.getCurrentPosition(
(position) => {
var date = new Date(position.timestamp);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var lat = parseFloat(position.coords.latitude), lon = parseFloat(position.coords.longitude);
var markerPos = {
latitude: lat,
longitude: lon
};
const {coordinate} = this.state
coordinate.timing({
latitude: 23.772290,
longitude: 90.3600850,
duration: 2000,
easing: Easing.linear,
},).start();
this.setState({
timestamp: hours + ':' + minutes + ':' + seconds,
region: {
latitude: lat,
longitude: lon,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
coordinate,
accuracy: position.coords.accuracy,
heading: position.coords.heading,
speed: position.coords.speed,
altitude: position.coords.altitude
});
},
(error) => alert(JSON.stringify(error)), {enableHighAccuracy: true, timeout: 20000, maximumAge: 3000}
);
this.watchId = navigator.geolocation.watchPosition(
(position) => {
var date = new Date(position.timestamp), hours = date.getHours(), minutes = date.getMinutes(), seconds = date.getSeconds();
var lat = position.coords.latitude, lon = position.coords.longitude;
var markerPos = {
latitude: lat,
longitude: lon
};
const {coordinate} = this.state;
coordinate.timing({
latitude: 23.772290,
longitude: 90.3600850,
duration: 2000,
easing: Easing.linear,
},).start();
this.setState({
timestamp: hours + ':' + minutes + ':' + seconds,
region: {
latitude: lat,
longitude: lon,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
},
coordinate,
accuracy: position.coords.accuracy,
heading: position.coords.heading,
speed: position.coords.speed,
altitude: position.coords.altitude
});
},
(error) => alert(JSON.stringify(error)), {enableHighAccuracy: true, timeout: 20000, maximumAge: 3000, distanceFilter: 2}
);
}
componentWillUnmount(){
navigator.geolocation.clearWatch(this.watchId);
}
render(){
console.log(this.state.coordinate);
return(
<View style={[styles.flexbox, styles.adjustCenter, styles.backdrop]}>
<MapView
style={styles.map}
region={{
latitude: 23.772046,
longitude: 90.3600703,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}}
showsPointsOfInterest={true}
pitchEnabled={true}
zoomEnabled={true}
showsBuildings={true}
showsTraffic={true}
showsIndoors={true}
showsScale={true}
toolbarEnabled={true}
showsUserLocation={true}
loadingEnabled={true}
loadingIndicatorColor='#6600ff'
onRegionChange={this.onRegionChange}
>
<Marker.Animated
coordinate={this.state.coordinate}
pinColor={'coral'}
title={'title'}
description={'description'}/>
</MapView>
</View>
);
}
}
export default WorldMap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment