Just got familiar with the Google Maps API (v2) and integrated it into my android app and i have 2 questions that i didn't find what is the best way to do it:
The first this is always focus on current user location (like GPS application). What i did is in the onCreate i get the user location (according to some stuff i found here and there))
@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);
}
Where listener
is a LocationListener
configured like this:
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);
}
};
Is this the correct way to do so?
The second thing is that the user can touch the map and dynamically add markers on the map (with his own titles and stuff). I tried to find (but didn't) a way to draw a path between the markers and act like a gps application. Is this even possible?
Thank you