1
我正在開發一個簡單的應用程序,顯示用戶在地圖上的位置。當他移動時,攝像機跟隨他將用戶保持在地圖的中心位置。一切似乎都很簡單,下面是相關的代碼。在Android上使用Google地圖的簡單跟蹤應用程序
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
LocationManager locationManager = null;
Location currentBestLocation = null;
LocationListener locationListener = null;
GoogleMap map = null;
Marker marker = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
currentBestLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
currentBestLocation = location;
if(map != null){
LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 17));
if(marker != null) marker.remove();
marker = map.addMarker(new MarkerOptions()
.position(position)));
}
}
.
.
.
};
}
@Override
public void onMapReady(GoogleMap map) {
this.map = map;
this.map.setMyLocationEnabled(true);
if(currentBestLocation != null)
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentBestLocation.getLatitude(), currentBestLocation.getLongitude()), 17));
}
@Override
protected void onStart() {
super.onStart();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
protected void onStop() {
super.onStop();
locationManager.removeUpdates(locationListener);
}
}
然而與此實現,我面臨着以下問題:雖然攝像頭跟隨用戶的動作,很多時候街道名稱層無法更新或有模糊/模糊的部分,其無法更新,直到我做地圖的手動滾動。然後整個地圖立即更新。我究竟做錯了什麼?
它似乎工作,但我必須在汽車旅行中嘗試它。我的實施有什麼問題?假設animateCamera不能用於這種情況下,是否有避免相機抖動產生的地方? – ssnape
你正在使用哪個手機(取決於它爲什麼是抽搐)?請做出回答,並接受它,如果它已經幫助你:-) – penta
舊的位置和新的位置之間的過渡並不平滑通過使用moveCamera,而不是animateCamera。我想如果有一種方法可以獲得平穩的運動 – ssnape