定位工具封装和使用 LocationCtrl

import React, {useState, useEffect} from ‘react’;
import {View, Text, ScrollView, StyleSheet, DeviceEventEmitter} from ‘react-native’;
import {Button, List, Switch} from ‘@ant-design/react-native’;
import {
LocationCtrl,
LayerManager,
MapManager,
MapView,
LocationEventType,
AutoPanMode,
LocationCtrl2,
LocationEventType2,
} from ‘@haibalai/react-native-arcgis’;
import {connect} from ‘react-redux’;
import WidthDrawer from ‘…/…/…/…/components/WithDrawer/index’;
import {Colors} from ‘react-native/Libraries/NewAppScreen’;
import {PermissionsUtil} from ‘…/…/…/…/…/nativeModules/util/PermissionsUtil’;
const MapId = ‘baseMap’;
const GraphicLayerId = ‘graphic点’;
const MapImageLayerId = ‘水闸-MapImageLayer’;
const FeatureLayerId = ‘水闸-featurelayer’;
const MapImageLayerIds = [
‘水闸-MapImageLayer’,
‘原水管线-MapImageLayer’,
‘海堤-MapImageLayer’,
‘海洋环境区划-MapImageLayer’,
];
const FeatureLayerIds = [
‘水闸-featurelayer’,
‘原水管线-featurelayer’,
‘原水管线-featurelayer’,
‘海洋环境区划-featurelayer’,
];
const GraphicLayerIds = [‘graphic点’, ‘graphic线’, ‘graphic面’];
const GeoX = 1.2664272368526626e7;
const GeoY = 2595847.2157473154;
const Geo4490X = 113.78868474700005;
const Geo4490Y = 22.693641836999973;
const locationEventType = LocationEventType.onLocationChanged;
let listenerId = ‘’;
let displayMode = AutoPanMode.COMPASS_NAVIGATION;
const symbolStr = ‘’;
const imageBse64 =
‘iVBORw0KGgoAAAANSUhEUgAAAFQAAABUCAYAAAAcaxDBAAAABHNCSVQICAgIfAhkiAAADgtJREFUeJy9nF9MXFUex7/MwDBU2g5CA10FTGNbXaRU7YqEAE/2QZA+GLNJG9jGyr60jTHpJhLbXS3dBzchmoj7YE21Je3DbrKbrjjJusmmTIOBFjVQsl1atsnQGgbDX4sw1Q7sQznDmXN/v3POvTP4TU7uuf/OOfczv9/5d8+dLPx8ysrwPSse0vNyjyt5echM57EeZTCBWzew6wlUl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ZgOLnVcl5YsL1C5a9Q0qDx0xxxyC5S7hwKgs1oQcWprkgkQB42zRg6adT3qBajuPluwYuvGMmXpYNhYXkatUpZXoLr7qbpPPW5jmW6BynGv57k8rJUuUF0aHFgurruekg6KCSi3zx2zViaAmtIygXILUpaNddlCSwuk0Hp8k6IDYgPLzYO5cdeMujan9fzIxwQvU96hA+L1nGdl0uXTzSMTZbGBtC4ghf4PMbnq5XFFjogAAAAASUVORK5CYII=’;
const delay = 100;
const period = 10000;
const enable = true;
const enableShow = false;
const opacity = 0.5;
const isShow = false;
let tmpLocateListener: any = null;
const initScale = 1000; //测试初始比例尺
const permissions = [‘android.permission.ACCESS_FINE_LOCATION’, ‘android.permission.ACCESS_COARSE_LOCATION’];
/** sideBar组件
*@param {*} renderCb renderCb是一个函数, 每次触发并且向里面传值(对象),content组件就以sideBarData的属性去接受
*/
const SideBar = ({renderCb, Home}: any) => {
const [content, changeContent] = useState(‘’);
const [errorContent, changeErrorContent] = useState(‘’);
const [singleMapImageLayerStatus, setSingleMapImageLayerStatus] = useState(false);
const [multiMapImageLayerStatus, setMultiMapImageLayerStatus] = useState(false);
const [singleFeatureLayerStatus, setSingleFeatureStatus] = useState(false);
const [multiFeatureLayerStatus, setMultiFeatureLayerStatus] = useState(false);
const [singleGraphicLayerStatus, setSingleGraphicLayerStatus] = useState(false);
const [multiGraphicLayerStatus, setMultiGraphicLayerStatus] = useState(false);
useEffect(() => {
renderCb({content, errorContent});
}, [content, errorContent, renderCb]);
const getGeoX = () => {
let coordinate = Home.coordinate;
if (coordinate === ‘4490’) {
return Geo4490X;
} else {
return GeoX;
}
};
const getGeoY = () => {
let coordinate = Home.coordinate;
if (coordinate === ‘4490’) {
return Geo4490Y;
} else {
return GeoY;
}
};
const onSwitchSingleMapImageLayer = (e: boolean) => {
if (e) {
LayerManager.showLayer(MapId, MapImageLayerId);
} else {
LayerManager.hideLayer(MapId, MapImageLayerId);
}
setSingleMapImageLayerStatus(e);
};
const onSwitchMultiMapImageLayer = (e: boolean) => {
if (e) {
LayerManager.showLayers(MapId, MapImageLayerIds);
} else {
LayerManager.hideLayers(MapId, MapImageLayerIds);
}
setMultiMapImageLayerStatus(e);
};
const onSwitchSingleFeatureLayer = (e: boolean) => {
if (e) {
LayerManager.showLayer(MapId, FeatureLayerId);
} else {
LayerManager.hideLayer(MapId, FeatureLayerId);
}
setSingleFeatureStatus(e);
};
const onSwitchMultiFeatureLayer = (e: boolean) => {
if (e) {
LayerManager.showLayers(MapId, FeatureLayerIds);
} else {
LayerManager.hideLayers(MapId, FeatureLayerIds);
}
setMultiFeatureLayerStatus(e);
};
const onSwitchSingleGraphicLayer = (e: boolean) => {
setSingleGraphicLayerStatus(e);
//加载单个图层的graphics
};
const onSwitchMultiGraphicLayer = (e: boolean) => {
setMultiGraphicLayerStatus(e);
//加载多个图层的graphics
};
const onStartAsyncNotShow = () => {
LocationCtrl.startAsync(MapId, false)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onCheckPermissions = () => {
PermissionsUtil.checkPermissions(permissions)
.then((r: any) => {
console.log(r, ‘当前请求定位权限结果状态!’);
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
console.log(e, ‘提出请求申请失败!’);
changeErrorContent(e);
});
};
const onStartAsyncShow = () => {
LocationCtrl.startAsync(MapId, true)
.then((r: any) => {
changeErrorContent(‘’);
LocationCtrl.startGetLocation(MapId, 50, 5000)
.then((r: any) => {
console.log(‘开启等间隔获取定位成功’, r);
changeErrorContent(‘’);
tmpLocateListener = DeviceEventEmitter.addListener(LocationEventType.onPassiveLocationChanged, res => {
changeContent(res);
});
})
.catch((e: any) => {
changeErrorContent(e);
});
})
.catch((e: any) => {
changeErrorContent(e);
});
// LocationCtrl.startAsync(MapId, true)
// .then((r: any) => {
// changeErrorContent(‘’);
// changeContent®;
// })
// .catch((e: any) => {
// changeErrorContent(e);
// });
};
const onSetTimerParam = () => {
LocationCtrl.setTimerParam(MapId, delay, period)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onStartListenCourseChange = () => {
LocationCtrl.startListenCourseChange(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onStopListenCourseChange = () => {
LocationCtrl.stopListenCourseChange(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onAddListener = () => {
LocationCtrl.addListener(MapId, locationEventType)
.then((r: any) => {
listenerId = r;
changeErrorContent(‘’);
tmpLocateListener = DeviceEventEmitter.addListener(locationEventType, r => {
changeContent®;
});
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onStartGetLocation = () => {
LocationCtrl.startGetLocation(MapId, 50, 5000)
.then((r: any) => {
console.log(‘开启等间隔获取定位成功’, r);
changeErrorContent(‘’);
tmpLocateListener = DeviceEventEmitter.addListener(LocationEventType.onPassiveLocationChanged, res => {
changeContent(res);
});
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onStopGetLocation = () => {
LocationCtrl.stopGetLocation(MapId)
.then((r: any) => {
console.log(‘关闭等间隔获取定位成功’, r);
changeErrorContent(‘’);
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onRemoveListener = () => {
LocationCtrl.removeListener(MapId, listenerId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onClearListener = () => {
LocationCtrl.clearListener(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onIsStarted = () => {
LocationCtrl.isStarted(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent(r.toString());
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onStopLocation = () => {
LocationCtrl2.stopLocation(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onGetHeading = () => {
LocationCtrl.getHeading(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onGetInitialZoomScale = () => {
LocationCtrl.getInitialZoomScale(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onSetInitialZoomScale = () => {
LocationCtrl.setInitialZoomScale(MapId, initScale)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onGetMapLocation = () => {
LocationCtrl2.getCurrentLocation()
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onSetAutoPanMode = () => {
LocationCtrl.setAutoPanMode(MapId, displayMode)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onGetAccuracySymbol = () => {
LocationCtrl.getAccuracySymbol(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onSetAccuracySymbol = () => {
LocationCtrl.setAccuracySymbol(MapId, symbolStr)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onGetAcquiringSymbol = () => {
LocationCtrl.getAcquiringSymbol(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onSetAcquiringSymbol = () => {
LocationCtrl.setAcquiringSymbol(MapId, symbolStr)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onGetCourseSymbol = () => {
LocationCtrl.getCourseSymbol(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onSetCourseSymbol = () => {
LocationCtrl.setCourseSymbol(MapId, symbolStr)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onGetDefaultSymbol = () => {
LocationCtrl.getDefaultSymbol(MapId)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};
const onSetDefaultSymbol = () => {
LocationCtrl.setDefaultSymbol(MapId, imageBse64)
.then((r: any) => {
changeErrorContent(‘’);
changeContent®;
})
.catch((e: any) => {
changeErrorContent(e);
});
};

React Native for Arcgis 地图开发 LocationCtrl (十五) - 小专栏