0
有没有办法通过访问加速计,指南针或其他方式来检测科尔多瓦的物理设备方向?在科尔多瓦检测设备方向
我想检测一个设备是侧向(即,横向)或直立(即纵向)。除了:cordova detecting orientation change我将无法通过window.orientation
使用定位,因为在用户锁定其方向时失败。
我不需要知道用户所处的模式。只需要知道设备是横向还是竖直。
有没有办法通过访问加速计,指南针或其他方式来检测科尔多瓦的物理设备方向?在科尔多瓦检测设备方向
我想检测一个设备是侧向(即,横向)或直立(即纵向)。除了:cordova detecting orientation change我将无法通过window.orientation
使用定位,因为在用户锁定其方向时失败。
我不需要知道用户所处的模式。只需要知道设备是横向还是竖直。
我找到了描述how device orientation can be read from accelerometer despite a locked device orientation的博客条目。
相关代码段在Objective-C提供的,而是自然适用于PhoneGap的,因为它可被翻译,并且经由Cordova's Device Motion Plugin使用。
这里的原始:
float x = -[acceleration x];
float y = [acceleration y];
float angle = atan2(y, x);
if(angle >= −2.25 && angle <= −0.75) {
//OrientationPortrait
} else if(angle >= −0.75 && angle <= 0.75){
//OrientationLandscapeRight
} else if(angle >= 0.75 && angle <= 2.25) {
//OrientationPortraitUpsideDown
} else if(angle <= −2.25 || angle >= 2.25) {
//OrientationLandscapeLeft];
}
说明:对于任何实际的参数x和y这两者不都是等于零,ATAN2(Y,X)是在正x之间的弧度的角度一个平面的轴和由它指定的坐标给出的点。该角度对于逆时针角度为正,对于顺时针角度为负。
的[科尔多瓦检测取向变化]可能的复制(http://stackoverflow.com/questions/28283061/cordova-detecting-orientation-change) – str
感谢您的指针。我编辑了这个问题。建议的解决方案不能解决锁定的设备方向。 – zaph0t