2014-10-10 17 views
1

我有一個谷歌地圖的Android應用程序。 有人可以告訴我,我們如何根據用戶當前的行走方向來旋轉地圖嗎?我已經閱讀了很多信息,但仍然有點混亂。根據當前行走方向旋轉地圖android谷歌地圖

謝謝

+0

你想看看方向傳感器......可能是他們的侷限性。 – zgc7009 2014-10-10 15:57:15

+0

可能重複[this](http://stackoverflow.com/questions/14767282/android-maps-v2-rotate-mapview-with-compass)和[this](http://stackoverflow.com/questions/1830391/旋轉mapview在Android) – Antrromet 2014-10-10 16:01:04

回答

1

您可以通過比較兩點來計算方位。你說你的用戶會走路,所以不應該太難讓兩個點相距一定距離。

當你有兩個點做一些數學像這樣

lon1 = degToRad(lon1); 
lon2 = degToRad(lon2); 
lat1 = degToRad(lat1); 
lat2 = degToRad(lat2); 

double a = Math.Sin(lon2 - lon1) * Math.Cos(lat2); 
double b = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1); 
double c = radToDeg(Math.Atan2(a, b)); // c is our bearing // 

這些都是我們的轉換功能

public static double degToRad(double deg){ 
    return deg * Math.PI/180.0; 
} 
public static double radToDeg(double rad){ 
    rad = rad * (180.0/Math.PI); 
    if (rad < 0) rad = 360.0 + rad; 
    return rad; 
} 

一旦你的軸承可以使用CameraUpdateFactory它傳遞給你的地圖。

map.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(LatLng, zoom, tilt, bearing))); 
+0

你的功能是這樣的:'float targetBearing = currentLocation.bearingTo(endingLocation)'? – pdcc 2014-10-13 10:08:31

+0

它爲我工作,謝謝 – Kokusho 2016-02-27 00:16:35