I am learning how to show google maps onto my app.Currently, I have done everything including adding meta-data tags and generating the API key but when I run my app it just shows me an empty fragment.No map is downloaded .
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
public static final int PERMISSION_REQUEST_CODE = 9001;
private static final int PLAY_SERVICES_ERROR_CODE = 9002;
public static final String TAG = "MapDebug";
private boolean mLocationPermissionGranted;
private GoogleMap mGoogleMap;
@RequiresApi(api = Build.VERSION_CODES.M)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment supportMapFragment= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
supportMapFragment.getMapAsync(this);
//isServicesOk();
//initGoogleMap();
// SupportMapFragment supportMapFragment=SupportMapFragment.newInstance();
// supportMapFragment.getMapAsync(this::onMapReady);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "Showing map", Toast.LENGTH_SHORT).show();
}
Xml Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_fragment"/>
</RelativeLayout>
Add
android:name="com.google.android.gms.maps.SupportMapFragment"
in your xml like
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map_fragment"/>
Edit
/**
* 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));
}
For more Information visit https://developers.google.com/maps/documentation/android-sdk/start#step_5_hello_map_take_a_look_at_the_code