1

I'm trying to add the google maps api into a fragment, first of all i create a project in the console of google to obtain a API KEY. When I have the API_KEY I put to the AndroidManifest.xml this lines:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyBhhB0AHhfiAMOjd9wRBKjckld-1ClMQ-Q"/>

Before of that, in my fragment.xmk I add the followin lines:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sergi.cycloguardian.Fragments.FragmentMap">

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

Finally in my fragment.java i have this line:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mView = inflater.inflate(R.layout.fragment_map, container, false);
    return mView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //Recuperamos la instancia del mapa
    mMapView = (MapView) mView.findViewById(R.id.map);
    if (mMapView != null) {
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);
    }

}

@Override
public void onMapReady(GoogleMap googleMap) {

    MapsInitializer.initialize(getContext());

    mGoogleMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions().position(new LatLng(40.689247, -74.044502)).title("State of liberty"));

    CameraPosition Liberty = CameraPosition.builder().target(new LatLng(40.689247, -74.044502))
            .zoom(16).bearing(0).tilt(45).build();
    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(Liberty));
}

But when I run the applicattion, the map doesnt appear. There is a grey screen with the icon of google.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Looks like you should be using the approach from the answer here: https://stackoverflow.com/questions/44687517/how-to-use-a-google-map-in-a-navigationdrawer – Daniel Nugent Apr 05 '18 at 15:50

0 Answers0