You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中Fragment内Google Maps的onMapReady方法未生效问题

Hey there! Let's figure out why your Google Map is stuck at the 0,0 coordinate (that's the off-Africa spot you're seeing) instead of your intended location. Since the map renders fine, the issue is almost certainly with how you're setting the camera position or initializing the map callback. Here are the most common fixes to check step by step:

1. Double-check your onMapReady camera setup

First, make sure you're actually telling the map where to center in the onMapReady callback. It's easy to accidentally leave the default 0,0 coordinates or mix up latitude/longitude order.

For a fixed target location (like a city), your code should look something like this:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    // Replace with your desired coordinates (latitude first, then longitude!)
    LatLng target = new LatLng(39.9042, 116.4074); // Example: Beijing
    // Zoom level 12 is a good starting point (higher = closer)
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(target, 12));
}

Critical note: LatLng takes (latitude, longitude) as parameters—mixing these up will send you to a totally wrong spot, even if the numbers are correct.

2. Ensure your Fragment's map initialization is correct

If you're using SupportMapFragment inside your Fragment, make sure you're properly fetching the map instance and attaching the callback. A common mistake is using the wrong FragmentManager.

In your Fragment's onViewCreated method:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Use getChildFragmentManager() for a Fragment inside another Fragment
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.map);
    if (mapFragment != null) {
        // Make sure your Fragment implements OnMapReadyCallback!
        mapFragment.getMapAsync(this);
    }
}

Don't forget to declare your Fragment implements OnMapReadyCallback—otherwise the callback won't fire, and the map stays at the default 0,0.

3. If you're targeting the user's current location: Check permissions and location services

If you want to show the user's live location instead of a fixed spot, you need to handle permissions and location retrieval correctly:

  • First, request dynamic permissions (required for Android 6.0+):
    private static final int LOCATION_PERMISSION_REQUEST = 100;
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            // Use FusedLocationProvider to get the user's last known location
            FusedLocationProviderClient locationClient = LocationServices.getFusedLocationProviderClient(requireContext());
            locationClient.getLastLocation()
                    .addOnSuccessListener(location -> {
                        if (location != null) {
                            LatLng currentLoc = new LatLng(location.getLatitude(), location.getLongitude());
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLoc, 15));
                        } else {
                            // Fallback to a default location if no location data is available
                            LatLng fallback = new LatLng(39.9042, 116.4074);
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(fallback, 12));
                        }
                    });
        } else {
            // Request permission if not granted
            ActivityCompat.requestPermissions(requireActivity(),
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    LOCATION_PERMISSION_REQUEST);
        }
    }
    
  • Also, verify that the user has enabled location services on their device—if it's turned off, getLastLocation() will return null.

4. Quick sanity check for gradle/Manifest configs

Even though you said you've set these up, it's worth a quick double-check:

  • In your app-level build.gradle, confirm you have the correct dependencies:
    dependencies {
        implementation 'com.google.android.gms:play-services-maps:18.2.0' // Use the latest version
        implementation 'com.google.android.gms:play-services-location:21.0.1' // If using location services
    }
    
  • In AndroidManifest.xml, ensure your Maps API key is present:
    <application ...>
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="YOUR_API_KEY_HERE"/>
    </application>
    
  • And don't forget the required permissions:
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    

Start with the first step—most of the time, the issue is just a missing camera move call or mixed-up coordinate order. If that doesn't fix it, work through the other checks one by one.

内容的提问来源于stack exchange,提问作者K.Best

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:29:55