在使用地圖定位跟需要用到RN定時器的時候,setInterval引用衝突了,這是導入例如高德地圖
import {
init,
Geolocation,
// setInterval,
setNeedAddress,
setLocatingWithReGeocode,
setDistanceFilter,
stop,
setOnceLocation,
addLocationListener,
} from "react-native-amap-geolocation";
async getLocation() {
await init({
ios: Constant.GaodeKey_Ios,
android: Constant.GaodeKey_Android
});
setNeedAddress(true); // android
setInterval(5000);// android,5 秒請求一次定位
setLocatingWithReGeocode(true); // ios
setDistanceFilter(10);// ios,設備移動超過 10 米纔會更新位置信息
watchId = Geolocation.watchPosition(
position => this.updateLocationState(position),
error => this.updateLocationState(error)
);
}
如果在頁面模塊中需要同時用到定時器setInterval的話,那麼這是就存在着衝突,定時器無效
可以採取的方法:
在getLocation方法中不需要引用react-native-amap-geolocation中的方法,
查找源碼發現setInterval是隻針對android端的,這是可以採用一下方法去進行調用:
let react_native_1 = require("react-native");
if (Constant.ANDROID)
react_native_1.NativeModules.AMapGeolocation.setInterval(5000);
如果不同的系統需要加載不同的組件,那麼就可以採用
/**
* 動態加載主界面
*/
render() {
const DidWebView = Platform.select({
ios: () => require('react-native').WebView,
android: () => require('react-native-webview-android'),
})();
return <DidWebView
source={
}
/>