Google Map is not displaying properly

  android, api, google-maps, java, maps

I’m writing an app in which I want to use Google Maps API for my Map Fragment and display user’s current location, but when I start the app it only shows the Google name and not the map. In the project I use both Java and Kotlin, but the Map Fragment is written in Java. I use SDK 30 with 6.5 Gradle. I added dependencies play-services-location and play-services-maps and I created an API KEY for my project. I don’t have a billing account, I don’t know if I need it to run my app. I use Pixel 2 emulator with Android 10 and both GPS and Wifi enabled.
Here is my fragment code:

public class MapFragment extends Fragment {

    private SupportMapFragment supportMapFragment;
    private FusedLocationProviderClient client;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_map, container, false);

        supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.google_map);

        client = LocationServices.getFusedLocationProviderClient(getActivity());

        getCurrentLocation();


        return view;
    }

    private void getCurrentLocation() {
        if (ActivityCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},44);
        }
        Task<Location> task = client.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if(location != null){
                    supportMapFragment.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap googleMap) {
                            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                            MarkerOptions options = new MarkerOptions().position(latLng)
                                    .title("You're here!");
                            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
                            googleMap.addMarker(options);
                        }
                    });
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 44){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                getCurrentLocation();
            }
        }
    }

Part of my AndroidManifest:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    
    <application
        (...)

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="(My API KEY)"/>
        <uses-library android:name="com.google.android.maps"/>

And logs for the moment I start the map fragment:

2021-01-12 21:45:56.679 31343-31343/com.example.covidapp I/Google Maps Android API: Google Play services package version: 204714040
2021-01-12 21:45:56.690 1783-9075/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 42729200 , only wrote 42729023
2021-01-12 21:45:56.705 31343-31558/com.example.covidapp E/Google Maps Android API: Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).
2021-01-12 21:45:56.709 31343-31343/com.example.covidapp W/xample.covidap: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
2021-01-12 21:45:56.709 31343-31343/com.example.covidapp W/xample.covidap: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
2021-01-12 21:45:56.710 31343-31343/com.example.covidapp W/xample.covidap: Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
2021-01-12 21:45:56.711 31343-31343/com.example.covidapp W/xample.covidap: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
2021-01-12 21:45:56.735 27351-27521/? W/GCoreFlp: No location to return for getLastLocation()
2021-01-12 21:45:56.736 31343-31559/com.example.covidapp D/HostConnection: HostConnection::get() New Host Connection established 0xe126b120, tid 31559
2021-01-12 21:45:56.737 31343-31559/com.example.covidapp D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2 
2021-01-12 21:45:56.743 31343-31559/com.example.covidapp D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2021-01-12 21:45:56.743 31343-31559/com.example.covidapp D/EGL_emulation: eglCreateContext: 0xc3f645c0: maj 1 min 0 rcv 1
2021-01-12 21:45:56.744 1793-1840/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 5554176
2021-01-12 21:45:56.775 31343-31559/com.example.covidapp D/EGL_emulation: eglMakeCurrent: 0xc3f645c0: ver 1 0 (tinfo 0xc3f693c0)
2021-01-12 21:45:56.867 1793-1840/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 5554176
     ```

Source: Android Questions

LEAVE A COMMENT