刚刚熟悉了 Google Maps API (v2) 并将其集成到我的 android 应用程序中,我有 2 个问题我没有找到最好的方法:
首先,始终关注当前用户位置(如 GPS 应用程序)。我所做的是在 onCreate 中我得到了用户位置(根据我在这里和那里找到的一些东西))
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
LatLng currentUserPosition = new LatLng(location.getLatitude(), location.getLongitude());
// setting the cam position to current position
CameraPosition camPosition = new CameraPosition(currentUserPosition, 17f, 30f, 0f);
CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition);
mapFragment.getMap().animateCamera(update);
// updating map every second
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, // 1-second interval.
10, // 10 meters.
listener);
}
哪里listener
是这样LocationListener
配置的:
private final LocationListener listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
CameraPosition camPosition = new CameraPosition(new LatLng(location.getLatitude(), location.getLongitude()), 17f, 30f, 0f);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition);
mapFragment.getMap().animateCamera(update);
}
};
这是正确的方法吗?
第二件事是用户可以触摸地图并在地图上动态添加标记(带有他自己的标题和东西)。我试图找到(但没有)一种在标记之间绘制路径并像 gps 应用程序一样工作的方法。这甚至可能吗?
谢谢