在Google Maps V2中,如何在加载地图时将相机移动到用户当前位置?

2St*_*ned 4 android google-maps

我想知道如何在加载地图时一次显示当前用户位置,而无需再次检查位置更改和摄像机移动,除非用户询问并单击。我看到了许多有关按钮单击事件和获取当前位置的教程,但是都没有帮助。我看到的另一个是旧的,不推荐使用的类(LocationClient等)。

任何帮助,将不胜感激!非常感谢!

Sag*_*ada 7

首先检查位置是否已启用。如果您的位置已启用,则地图将自动加载。否则没有。

这是我的两件事代码。

   @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);
    }
Run Code Online (Sandbox Code Playgroud)

此代码仅适用于Google Map v2。

当您要启用GPS设置时,您的地图处于onPause()状态,因此请尝试处理android的生命周期。

如果需要,将此行放在OnResume()状态上。

@Override
    protected void onResume() {
        super.onResume();
        mMapFragment.getMapAsync(this);
    }
Run Code Online (Sandbox Code Playgroud)