I have Google Maps set up with my application and can view the map with a blue dot indicating my location on the map, however everything I have tried to centre and zoom in on that location automatically does not seem to work.
When I press the target icon in the top right the camera view zooms in to my location which is what I want it to do automatically.
I have tried the following:
LatLng latlngPos = new LatLng(location.getLatitude(), location.getLongitude())
myMap.moveCamera(CameraUpdateFactory.newLatLng(latlngPos));
myMap.animateCamera(CameraUpdateFactory.zoomTo(15));
And also tried
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngPos, 15));
Yet neither seem to be working. I have these lines of code in onLocationChanged as shown in a tutorial I have followed. I have attempted different suggestions from similar questions on here also and it still seems to be zoomed out, yet highlighting my current location in the correct position by a blue dot on the map.
Have I missed something obvious? Thanks
Edit: Entire activity:-
public class FindBankActivity extends Activity implements LocationListener {
private GoogleMap myMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Make sure Google Play Services available
if(isGooglePlay()){
setContentView(R.layout.activity_find_bank);
setUpMap();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.find_bank, menu);
return true;
}
public boolean isGooglePlay() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status == ConnectionResult.SUCCESS) {
return(true);
}
else
{
((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 15)).show();
//Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show();
}
return(false);
}
public void setUpMap() {
if(myMap == null) {
myMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if(myMap != null) {
//Initiate map
myMap.setMyLocationEnabled(true);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = lm.getBestProvider(new Criteria(), true);
if(provider == null) {
onProviderDisabled(provider);
}
Location loc = lm.getLastKnownLocation(provider);
if(loc == null) {
LocationListener locListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat,lng);
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 15));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(provider, 20000, 0, locListener);
}
if(loc != null) {
onLocationChanged(loc);
}
}
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onLocationChanged(Location location) {
//Get the location
LatLng latlngPos = new LatLng(location.getLatitude(), location.getLongitude());
//Display on the map and zoom in
// myMap.moveCamera(CameraUpdateFactory.newLatLng(latlngPos));
// myMap.animateCamera(CameraUpdateFactory.zoomTo(15));
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlngPos, 15));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}