Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps not showing Android

I want to implement google maps in my app. I added a google maps activity and created a key. I didn't change anything in the code elsewhere. I should work but it doesn't.

MapsActivity

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kevin.map.MapsActivity" />
like image 540
kevinh1998 Avatar asked May 02 '26 09:05

kevinh1998


2 Answers

Even after you follow steps from Official documentation or you're done with steps from Android Studio, and you're still not able to see the map, follow these steps to ensure whether it is a problem with API key or not:

  1. Filter with 'Google Maps' in your logcat.
  2. You will see an error message, which contains the correct package name & SHA key which should be present as it is in the API Key
  3. Go to Google Cloud Console -> API -> Credentails, and cross-verify both the above, correct if necessary.
  4. Wait for 10-15 minutes for the API Key changes to be reflected
like image 82
Mohammed Mukhtar Avatar answered May 04 '26 22:05

Mohammed Mukhtar


In addition to other posts, you may refer with this thread. The reason may be the API key used was created with the wrong keystore. You need to make sure you use your debug keystore when you create an API key in the Google API console. Based from this link, when using GoogleMaps for Android, you need two keys - debug and release. The "debug" key is kind of a misleading term. This key is also to be used when you develop the app. So essentially, use the debug key for development, testing, debugging. When you're ready to launch the app to Market, set the android:debuggable="false" in the AndroidManifest.xml and use the Signed API key.

like image 27
abielita Avatar answered May 04 '26 22:05

abielita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!