问题描述
我想限制用户将标记/大头针拖动到特定的Km之外,以进行本机反应。我正在使用react-native-maps显示地图,并使用圆圈显示半径。它在iOS上运行良好,只要我将图钉拖动到圆圈之外,它就会提示我,但对于android,提示后会相差200-300米。这是代码,
<MapView
provider={PROVIDER_GOOGLE}
style={{ height: mapHeight }}
region={region}
ref={_map}
zoomEnabled={true}
minZoomLevel={9}
onMapReady={() => console.log('ready')}
onRegionChangeComplete={(region) => _onRegionChangeComplete(region)}
onDoublePress={_setMapDragging}
onPanDrag={() => setMapDragging(true)}
>
{renderBoundaryCircle()}
</MapView>
const renderBoundaryCircle = () => {
return (
<Circle
center={{ latitude: initialAddressLatitude,longitude:
initialAddressLongitude }}
radius={Platform.OS == 'android' ? 2200 : 2050}
strokeWidth={2}
strokeColor={'#1a66ff'}
lineJoin={'round'}
fillColor={'rgba(230,238,255,0.5)'}
ref={_circleRef}
/>
);
};
const _onRegionChangeComplete = (region: RegionObject) => {
const diffinkm = distanceBwTwoLatLng(
initialAddressLatitude,initialAddressLongitude,latitude,longitude
);
if (diffinkm > 2) {
alert("beyond 2 km");
} else {
if (isMapDragging || Platform.OS == 'ios') {
setMapDragging(false);
setRegion(region);
setLatitude(region.latitude);
setLongitude(region.longitude);
console.log(region.latitude,region.longitude);
}
//this check is put for zoom,even if I remove this check,nothing changes
if (!isMapDragging && Platform.OS == 'android') {
return;
}
}
};
//function to calculate the distance between the initial lat-long and newlat-long
const distanceBwTwoLatLng = (lat1: number,lon1: number,lat2: number,lon2: number) => {
const deg2rad = (deg: number) => deg * (Math.PI / 180);
const R = 6371; // Radius of the earth in km
const dLat = deg2rad(lat2 - lat1); // deg2rad below
const dLon = deg2rad(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1 - a));
const d = R * c; // distance in km
console.log(`distance in km(s): ${d}`);
return d;
};
请提出一些解决方案。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)