Android 将地图居中放置在设备上';当前位置,不带";“中央按钮”;

Android 将地图居中放置在设备上';当前位置,不带";“中央按钮”;,android,google-maps,Android,Google Maps,我在网络上做了很多研究,但我可以;我找不到关于这个话题的任何东西。不管是谁,我有一张地图可以找到设备的当前位置,代码如下: mMap.setMyLocationEnabled(false); mMap.getUiSettings().setMyLocationButtonEnabled(false); 我禁用了以设备位置为中心的按钮;有没有办法不用按钮把地图居中?因此,当地图加载时,它会自动居中到设备的位置。因此,该按钮的作用是找到您当前的lat和long,并根

我在网络上做了很多研究,但我可以;我找不到关于这个话题的任何东西。不管是谁,我有一张地图可以找到设备的当前位置,代码如下:

        mMap.setMyLocationEnabled(false);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);

我禁用了以设备位置为中心的
按钮
;有没有办法不用按钮把地图居中?因此,当地图加载时,它会自动居中到设备的位置。

因此,该按钮的作用是找到您当前的lat和long,并根据该按钮固定地图相机的位置。如果您知道要居中的位置的纵横比,则可以使用以下代码-

CameraPosition target;
latLngTemp = new LatLng(-36.84490439080399, 174.76745902901663);
                target = CameraPosition.builder( ).target( latLngTemp ).zoom( 10 ).build( );
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(target));

因此,现在设备屏幕上的地图以上述坐标为中心

您可以使用以下代码

CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(new LatLng(/*current latitude*/location.getLatitude(), /*current longitude*/location.getLongitude()))      // Sets the center of the map to location user
        .zoom(17)                   // Sets the zoom
        .build();                   // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

有关更多信息,请访问

您应该实现LocationListener以获取位置更新。在onLocationChanged(位置)中心,您可以像这样使用摄像头

@Override
public void onLocationChanged(Location location) {
   map.animateCamera(CameraUpdateFactory.newCameraPosition(new LatLong(location.getLatitude(), location.getLongitude())); 
}
有关如何实现LocationListener,请参见此