0

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

}

}
rjc
  • 13
  • 5

2 Answers2

0

I guess you forgot to add the MAP-UI Settings

myMap.getUiSettings().setZoomControlsEnabled(true);
  • Thanks but I already have the manual Zoom controls enabled so I can click the + or - to zoom in/out. I want it to automatically zoom into my location when the activity starts. – rjc Mar 11 '14 at 18:36
  • Past your entire activity please, I've used Google Maps quite a bit and Zooming I seem to know a lot about. The code you pasted in your question seems correct. So please paste your entire activity so I can see how you're implementing it. – KickAss Mar 11 '14 at 20:40
  • @rjc I see you've put the above code inside `onLocationChanged`. Why? If you want to move it automatically, keep it outside the LocationListener. From what it seems, are you sure the onLocationChanged is being called? Have you tested it? Try putting a `Toast` inside onLocationChanged() and see if it displays. – KickAss Mar 12 '14 at 20:41
  • @rjc also see this post if `onLocationChanged` is not called: http://stackoverflow.com/questions/8447861/onlocationchanged-is-not-called-automatically – KickAss Mar 12 '14 at 20:43
0

You have followed a wrong tutorial. If you want to get updates on blue dot location changes, use OnMyLocationChangeListener with your GoogleMap object instead of LocationManager.

These methods can return different results because GoogleMap uses fused location provider and LocationManager only raw GPS or network provider.

If you are worried about OnMyLocationChangeListener being deprecated, see my monologue here.

Community
  • 1
  • 1
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94