Skip to content

Instantly share code, notes, and snippets.

@voratham
Last active January 23, 2018 05:14
Show Gist options
  • Save voratham/9f95f8db7e87dd1547a251b22384b761 to your computer and use it in GitHub Desktop.
Save voratham/9f95f8db7e87dd1547a251b22384b761 to your computer and use it in GitHub Desktop.
react-native-maps getCurrentLocation and general rules => Not Move when add multiple marker via socket , setInterval
<MapView
style={{...StyleSheet.absoluteFillObject}}
ref={map => {
this.map = map;
}}
minZoomLevel={2}
maxZoomLevel={20}
provider={MapView.PROVIDER_GOOGLE}
initialRegion={this.state.initialRegionState}
showsUserLocation={true}
onMapReady={() => {
console.log(this.map)
Platform.OS === 'ios' && this.map.animateToRegion(this.state.initialRegionState, 0.1);
}}
onRegionChangeComplete={ this._updateRegionChange.bind(this)}
>
/* ไม่จำเป็นที่จะต้อง set region property ไว้ เพราะนั้นหมายถึง การ lock
General Rules คือไม่เอา region พึ่งพาเป็น state ให้ใช้วิธีการอื่นแทน เพราะมันจะ lag และไม่สามารถทำงานได้ ติด Loop เสี่ยงต่อการกืิน cpu and ram.
Ideal สำหรับการ // set position current ของ device
https://github.com/react-community/react-native-maps/issues/209#issuecomment-350907665
OR ทำปุ่ม แล้วมี event เรียก
*/
`
_getCurrentLocation(){
console.log('getCurrentLocation')
navigator.geolocation.getCurrentPosition(
(position)=>{
const { coords : { latitude , longitude} } = position;
const region= { latitude , longitude , latitudeDelta : LATITUDE_DELTA , longitudeDelta : LONGITUDE_DELTA };
this.setState({ initialRegionState : region } , () => {
console.log(this.state.initialRegionState)
console.log(this.state.regionTemp)
// call function set map current position.
Platform.OS === 'ios' && this.map.animateToRegion(this.state.initialRegionState, 0.1);
})
},
(error)=> console.log(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge:1000}
)
}
`
// react native maps v0.19 , react-native 0.51 , react 16
// ref : https://github.com/react-community/react-native-maps/issues/1966
ทำตัว get current user position
<MapView
showsMyLocationButton={true} // native from lib
/>
_lockCurrentPosition = () => {
if(!this.mapRefs ) return;
console.log('pass mapRefs :: ', this.mapRefs)
if( Platform.OS === 'ios'){
console.log('ios condition')
this.mapRefs.animateToRegion(this.appStore.user.region, 0.1); // ios
}else{
console.log('android condition')
this.mapRefs.animateToRegion(this.appStore.user.region); // android
}
}
ref: https://github.com/react-community/react-native-maps/issues/209#issuecomment-350907665
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment