如何在 Android 中用自定义图标替换 Google 地图当前位置蓝色图标

问题描述 投票:0回答:1

这是我获取当前位置的代码。如果有人知道这样做的方法,请提供帮助。我被困住了。

    private fun enableMyLocation() {
        if (isPermissionGranted()) {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.ACCESS_FINE_LOCATION
                ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return
            }
            map.isMyLocationEnabled = true
        } else {
            ActivityCompat.requestPermissions(
                this, arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION),
                REQUEST_LOCATION_PERMISSION
            )
        }
    }
android google-maps
1个回答
0
投票

在 Google 地图中,您无法直接更改地图上指示您当前位置的蓝点。解决此问题的方法是隐藏蓝点并在用户当前位置添加您自己的自定义标记。

     FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(/* provide context here = */ this);

    // Get the current location
    fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
        if (location != null) {
            LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
            
            // Add a custom marker at the user's current location
            mMap.addMarker(new MarkerOptions()
                .position(currentLocation)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_icon)) // Replace with your custom icon
            );
            
            // Move the camera to the user's current location
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.