【问题标题】:Move camera to current location OnMapReady (Android Google Maps API)将相机移动到当前位置 OnMapReady (Android Google Maps API)
【发布时间】:2018-09-13 22:22:02
【问题描述】:

如何让相机在地图打开后立即移动到当前用户位置?
这就是我需要该位置许可的方式:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } else {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_LOCATION_REQUEST_CODE);
    }

这是在 onCreate 函数中

【问题讨论】:

  • 试试我编辑的答案。
  • 我的回答对您有帮助吗?如果是,请将其标记为有用的答案,以便遇到相同问题的每个人都可以在这里看到解决方案。谢谢。

标签: java android google-maps google-maps-api-3 maps


【解决方案1】:

此代码应使地图居中位置,您需要地理位置。

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(**LatLng object of your position**)      // Sets the center of the map to Geolocation
.zoom(17)                   // Sets the zoom
.bearing(90)                // Sets the orientation of the camera to east
.tilt(30)                   // Sets the tilt of the camera to 30 degrees
.build();                   // Creates a CameraPosition from the builder

map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

编辑: 首先我们在顶部创建一些变量:

private boolean localizationAllowed; //here we save if the user has allowed to locate him.
private boolean localizationRequested = false; // here we save if the localization has already requested. 

创建一些私有方法:

private void enableLocationUpdates(String provider) {
        if(!localizationRequested) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS},
                        MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATIONS);
                localizationRequested = true;
            }
        }
        if (provider != null && localizationAllowed) {
            locationManager.requestLocationUpdates(provider, 1000, 0, this);
        }
    }

    private void disableLocationUpdates() {
        if(!localizationRequested) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS},
                        MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATIONS);
                localizationRequested = true;
            }
        }
        if (locationManager != null && localizationAllowed){
          locationManager.removeUpdates(this);
        }
    }
private void initLocalisation(){
    if(localizationAllowed) {
        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (service != null) {
            boolean enabled = service
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // check if enabled and if not send user to the GSP settings
            // Better solution would be to display a dialog and suggesting to
            // go to the settings
            if (!enabled) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }

            // Get the location manager
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            // Define the criteria how to select the locatioin provider -> use
            // default
            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);

            enableLocationUpdates(provider);
        }
    }
} 

初始化本地化:

@Override
protected void onCreate(Bundle savedInstanceState) {
 //your onCreate methods
 initLocalisation();
 //your onCreate methods
}

onMapReady 在地图准备好时开始本地化

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Location location = getLastLocation();
    if (location != null) {
        LatLng you = new LatLng(location.getLatitude(), location.getLongitude());
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(you, 17f));
    }
}

【讨论】:

  • 其实我需要地理定位部分。我知道如何移动相机,但我不知道如何获取用户当前位置的 LatLng。
猜你喜欢
  • 2021-10-09
  • 1970-01-01
  • 2015-03-13
  • 2013-08-27
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多