map.setmylocationenabled(true) 不工作

map.setmylocationenabled(true) not working

我正在 android 应用程序中使用 Google 地图。我需要将地图重新​​定位到客户的当前位置。我使用了以下语句 -

map.setmylocationenabled(true);

这会在右上角显示一个按钮,但单击该按钮不起作用。

按钮点击监听器:

mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                @Override
                public boolean onMyLocationButtonClick() {
                    mMap.addMarker(new MarkerOptions().position(myLatLng).title("My Location"));
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, zoomLevel));
                    return false;
                }
            });

你有没有试过获取你的经纬度,使用后 setmylocationenabled(true)?

例子

gMap.setMyLocationEnabled(true);
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

您现在可以使用您的纬度和经度,并将相机动画设置到您get.Hope 它有帮助的 lat/lng 位置。

只需从 获取代码,然后修改您的按钮点击侦听器以请求另一个位置:

         mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                @Override
                public boolean onMyLocationButtonClick() {
                     if (mGoogleApiClient != null) {
                         LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
                     }
                     return false;
                }
            });

onLocationChanged() 中的代码将 re-center 相机位置,然后 un-register 再次进行位置更新:

@Override
public void onLocationChanged(Location location)
{
    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

    //move map camera
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));

    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
}

最后一行是我的解决方案:

myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);

您应该添加 setMyLocationEnabled (boolean enabled) .

Enables or disables the my-location layer.

While enabled and the location is available, the my-location layer continuously draws an indication of a user's current location and bearing, and displays UI controls that allow a user to interact with their location (for example, to enable or disable camera tracking of their location and bearing).

In order to use the my-location-layer feature you need to request permission for either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION unless you have set a custom location source.

演示

您应该将其添加到 onMapReady (GoogleMap googleMap) 部分。

 @Override
        public void onMapReady(GoogleMap googleMap) {

            googleMapOBJ = googleMap;
            if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            googleMapOBJ.setMyLocationEnabled(true);
            googleMapOBJ.getUiSettings().setMyLocationButtonEnabled(true);

Kotlin 方式

mMap = googleMap
mMap.isMyLocationEnabled = true
mMap.uiSettings.isMyLocationButtonEnabled = true

科特林 -

mMap!!.isMyLocationEnabled = true
mMap!!.uiSettings.isMyLocationButtonEnabled =  true

Java -

myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true); 

位置数据图层在地图的右上角添加了一个我的位置按钮。当用户点击按钮时,地图以设备位置为中心。如果设备静止,位置显示为蓝点;如果设备移动,位置显示为蓝色 V 形。

map.setMyLocationEnabled(布尔值) 调用需要权限,代码应明确检查权限是否可用。

或者,您可以使用注释抑制缺少的位置权限。

您可以使用新的 Activity Result API

轻松实现此目的

这是代码

class MapsActivity : AppCompatActivity() , OnMapReadyCallback {

private lateinit var map: GoogleMap


//suppress missing permission
@SuppressLint("MissingPermission")
private val locationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) {

    isGranted ->

    if (isGranted) {

        //add my Location Button on top-right side corner
        map.isMyLocationEnabled = true
    }
    else {

        ActivityCompat.requestPermissions(this ,
                arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION) ,
                0)
    }
}

ActivityResultCallback 定义应用程序如何处理用户对权限请求的响应。

要显示系统权限对话框,请在 ActivityResultLauncher 实例上调用 launch() 方法。

override fun onMapReady(googleMap: GoogleMap) {
    map = googleMap

    //call launch() passing in the permission required
    locationPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION)}

launch()调用后,系统权限对话框出现。当用户做出选择时,系统异步调用 ActivityResultCallback 的实现。

我在我的真实设备中遇到了同样的问题,甚至我 add permission 的位置。尽管如此,它仍然无法正常工作,我只是手动打开我设备中的位置它工作得很好。