Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move camera to user current location as map loads, in Google maps V2?

I wonder how can i achieve showing current user location once as the map loads, without checking ever again for location change and camera moving, Unless the user asked and clicked for it. I saw many tutorials about button clicking event and getting current loc , But none of them helped. The other i saw was old with deprecated classes( LocationClient, etc...).

Any help would be appreciated! Thanks a lot!

like image 937
2Stoned Avatar asked Jun 19 '16 07:06

2Stoned


1 Answers

first check if location is enable or not.. if your location is enable then map will load automatically. otherwise not.

here is my code for both thing..

   @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);

            mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);

            try {
                manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(myIntent);
                    overridePendingTransition(R.anim.push_up_in,
                            R.anim.push_up_out);
                } else {
                    mMapFragment.getMapAsync(this);
                    overridePendingTransition(R.anim.push_up_out,
                            R.anim.push_up_in);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

 @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        if (isLocationEnabled(MapsActivity.this)) {
            manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            mCriteria = new Criteria();
            bestProvider = String.valueOf(manager.getBestProvider(mCriteria, true));

            mLocation = manager.getLastKnownLocation(bestProvider);
            if (mLocation != null) {
                Log.e("TAG", "GPS is on");
                final double currentLatitude = mLocation.getLatitude();
                final double currentLongitude = mLocation.getLongitude();
                LatLng loc1 = new LatLng(currentLatitude, currentLongitude);
                mMap.addMarker(new MarkerOptions().position(loc1).title("Your Current Location"));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLatitude, currentLongitude), 15));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            }
        } else {
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            manager.requestLocationUpdates(bestProvider, 1000, 0, (LocationListener) this);
        }
        setupMap();
    }

    private void setupMap() {

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
                android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        AppSettings settings = AppSettings.getSettings(getApplication());
        String m = settings.getMapType();
        try {
            if (m.contentEquals(null)) {
                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            } else if (m.contentEquals("NORMAL")) {
                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            } else if (m.contentEquals("HYBRID")) {
                mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            } else if (m.contentEquals("SATELLITE")) {
                mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            } else if (m.contentEquals("TERRAIN")) {
                mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            } else {
                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setCompassEnabled(true);
        mMap.getUiSettings().setIndoorLevelPickerEnabled(true);
        mMap.setBuildingsEnabled(true);
        mMap.setIndoorEnabled(true);
    }

this code is for only google map v2.

and when you are going to enable you GPS setting, your map is in onPause() state so try to handle android life cycle.

put this line on OnResume() state if its needed..

@Override
    protected void onResume() {
        super.onResume();
        mMapFragment.getMapAsync(this);
    }
like image 92
Sagar Chavada Avatar answered Nov 14 '22 21:11

Sagar Chavada