0
我剛剛因CameraUpdateFactory出於某些原因出現了一些問題。顯示位置並將相機居中對準的問題
所以我併入在GpsButton一個onclickListener此代碼上的導航片段:
if (ContextCompat.checkSelfPermission(mapFragment.getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
Log.d("Permission checked", "checkSelfPermission passed with no errors");
map.setMyLocationEnabled(true);
Log.d("Permission checked", "Location Layer implementation successful");
} else {
//Request the Permission
ActivityCompat.requestPermissions(mapFragment.getActivity(), new String[]{
Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
這基本上使得要被顯示爲僅當按下GPS按鈕地圖上的藍色圓點的位置。迄今爲止,這已經是完美的功能。
我也納入的方法將攝像機移動到我的當前位置:
public void locateMe() {
LocationManager locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); // Getting LocationManager object from System Service LOCATION_SERVICE
Criteria criteria = new Criteria();// Creating a criteria object to retrieve provider
String provider = locationManager.getBestProvider(criteria, true);// Getting the name of the best provider
if (ContextCompat.checkSelfPermission(mapFragment.getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mapFragment.getActivity(), new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 2);
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
double latitude = location.getLatitude(); //Getting latitude of the current location
double longitude = location.getLongitude(); // Getting longitude of the current location
myPosition = new LatLng(latitude, longitude); // Creating a LatLng object for the current location
map.moveCamera(CameraUpdateFactory.newLatLngZoom(myPosition, CAMPUS_DEFAULT_ZOOM_LEVEL));//Camera Update method
}
}
出於某種原因,這是擊中或錯過。 3天前,它鎖定的位置不是我目前所在的位置,而在過去的兩天內它完美運行。沒有任何代碼被改變。有人可以解釋發生了什麼事嗎?任何修復或建議,將不勝感激。
我明白了,但其中一個問題似乎是,它正確地獲得我的位置,然後顯示我的位置,我已經回到了一個小時。這是正常的嗎? – BustyChicksFTW
這個位置有時是正確的,其他時間不正確是正常的。如果其他應用程序最近沒有請求過某個位置,則您的位置可能會過時,而其他時間則是正確的。但是,如果您的位置正確,通常在幾分鐘後應該是正確的。您應該記錄使用哪個LocationProvider,因爲您使用getBestLocationProvider,每次調用此方法時可能會返回一個不同的位置提供程序,並且準確度可能會更高或更低,因爲這是不同的位置。 – Rockney
有沒有辦法以攝像頭鎖定googlemap.setmyLocationEnabled編程? – BustyChicksFTW