Android上使用谷歌地图的简单跟踪应用程序
我正在开发一个简单的应用程序,在地图上显示用户的位置。当他移动时,相机会跟随他,让用户保持在地图的中心。一切看起来都很简单,下面是相关代码Android上使用谷歌地图的简单跟踪应用程序,android,google-maps,Android,Google Maps,我正在开发一个简单的应用程序,在地图上显示用户的位置。当他移动时,相机会跟随他,让用户保持在地图的中心。一切看起来都很简单,下面是相关代码 public class MainActivity extends FragmentActivity implements OnMapReadyCallback { LocationManager locationManager = null; Location currentBestLocation = null; Locatio
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);
}
}
然而,在这个实现中,我面临着以下问题:尽管相机跟踪用户的移动,但通常街道名称层无法更新,或者有一个模糊/模糊的部分无法更新,直到我手动滚动地图。然后整个地图立即更新。我做错了什么?在您的
onLocationChanged
中使用此代码
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
这似乎管用,但我还得在开车旅行时试试。我的实施中有什么问题?如果animateCamera不能在这种情况下使用,是否有一种方法可以避免由此导致的摄像头抖动?您使用的是哪款手机(取决于它抖动的原因)?如果答案对您有帮助,请投票并接受:-)使用moveCamera而不是animateCamera,旧位置和新位置之间的过渡不平滑。我想如果有一种方法可以获得平稳的移动,你使用的是什么设备?也许它无法解析所有沉重的地图数据,请尝试退出所有应用程序