我从这个链接取得指南针的代码。如何将我的指南针指向麦加?
http://www.androidcode.ninja/android-compass-code-example/
如何在我的指南针设置圣地的位置?
我需要做些什么来将我的指南针指向麦加?
我从这个链接取得指南针的代码。如何将我的指南针指向麦加?
http://www.androidcode.ninja/android-compass-code-example/
如何在我的指南针设置圣地的位置?
我需要做些什么来将我的指南针指向麦加?
你需要确定你的位置相对于麦加,所以你将需要Location permission
和实施Location Updates
。
您可以实现使用:Receveing location updates
有了这个信息,您可以确定在麦加将是对你的指南针,并进行相应的设置。为了确定角度,使用此代码:
private double angleFromCoordinate(double lat1, double long1, double lat2,
double long2) {
double dLon = (long2 - long1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
* Math.cos(lat2) * Math.cos(dLon);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
brng = 360 - brng; // count degrees counter-clockwise - remove to make clockwise
return brng;
}
好吧,我有一个角度。但是,从那个角度我怎么把麦加图像放在我的指南针上。 –
好了,这是一个长期问题,很少说话。答案是扩展示例中的OnSensorChanged代码,以计算从当前位置到麦加的标题,并根据传感器面对的当前方向调整标题。您需要GPS和当前位置坐标固定麦加的合作方式,计算相对于北方的方向(指南针设计指向的方式),然后根据当前的标题进行调整。
http://math.stackexchange.com/questions/796243/how-to-determine-the-direction-of-one-point-from-another-given-their-coordinate –