2017-06-05 1 views
1

Dans mon application Android, je veux avoir une MainActivity avec une carte google occupant tout l'écran.Google map ne montre pas dans l'activité android?

Mon problème est que la carte Google ne présente pas une carte, seul le logo google dans le coin inférieur gauche:

enter image description here

Voici les parties pertinentes de l'activité principale où la carte doit être affichée:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, 
     NavigationView.OnNavigationItemSelectedListener { 

    private GoogleMap mMap; 

    private FragmentManager mFragmentManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     setupLayout(); 

     mFragmentManager = getSupportFragmentManager(); 
    } 

    /** 
    * 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)); 
    } 

Voici le code XML pour le contenu de l'activité principale qui détient le fragment google map:

<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.purringcat.stray.view.activity.MainActivity"/> 

Voici le fichier manifeste:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.purringcat.stray"> 

    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

    <!-- 
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
     Google Maps Android API v2, but you must specify either coarse or fine 
     location permissions for the 'MyLocation' functionality. 
    --> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <application 
     android:name=".Stray" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".view.activity.MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 

       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".view.activity.LoginActivity" 
      android:label="@string/title_activity_login" 
      android:theme="@style/AppTheme.NoActionBar"> 
     </activity> 
     <activity android:name=".view.activity.SignUpActivity"> 
     </activity> 
     <!-- 
      The API key for Google Maps-based APIs is defined as a string resource. 
      (See the file "res/values/google_maps_api.xml"). 
      Note that the API key is linked to the encryption key used to sign the APK. 
      You need a different API key for each encryption key, including the release key that is used to 
      sign the APK for publishing. 
      You can define the keys for the debug and release targets in src/debug/ and src/release/. 
     --> 
     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key"/> 

    </application> 

</manifest> 
+1

Ceci est généralement dû à une mauvaise intégration des cartes (mauvais jetons/clés/certificats) –

+0

Cela peut-il être dû au fait que je n'utilise pas FragmentActivity? –

+1

avez-vous mis ces lignes dans votre manifeste ???? < méta-données android: name = "com.google.android.gms.version" android: valeur = "@ integer/google_play_services_version" /> – Kushan2

Répondre

1

Vous devez activer la carte google api de la console Google pour votre projet. Et vous devez également télécharger le fichier json à partir de la console.

1. Aller à google console

2. Cliquez sur votre projet.

3. Cliquez sur la section de la bibliothèque dans le panneau de menu de gauche. voir image ci-dessous

enter image description here

  1. Dans google maps api, cliquez sur Google android api

  2. qu'habiliter que.

  3. maintenant Aller firebase console

  4. Cliquez sur vos paramètres du projet et de projet goto et télécharger le fichier JSON.

  5. Ajoutez ce fichier json dans votre application.

+0

Je sais que, je l'ai juste littéralement activé sur la console google, puis ajouté la clé API au manifeste. –

+0

Comment activer la carte? –

+0

Il est dit déjà activé. –

1

Ajouter cette autorisations à menifest

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

OU Vérifiez dans la console API Google est ENABLED

OU Vérifiez votre GOOGLE_API_KEY

+0

J'ai inclus la question pour inclure le manifeste J'ai activé l'API et ajouté la touche api qui commence par AIz –

1

Je viens de vérifier sur la carte google comme la vôtre.

Il suffit de comparer votre code avec le mien.

MainActivity.java

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)); 
} 

}

Et Manifestez

<?xml version="1.0" encoding="utf-8"?> 

<!-- 
    The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
    Google Maps Android API v2, but you must specify either coarse or fine 
    location permissions for the 'MyLocation' functionality. 
--> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <!-- 
     The API key for Google Maps-based APIs is defined as a string resource. 
     (See the file "res/values/google_maps_api.xml"). 
     Note that the API key is linked to the encryption key used to sign the APK. 
     You need a different API key for each encryption key, including the release key that is used to 
     sign the APK for publishing. 
     You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    --> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

Note: Vous devez créer un projet dans google

click here to visit the google console

Remplacez votre valeur clé google_maps_api

<resources> 


<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Your Key</string> 

espère que cela fonctionne pour vous. PS: vous pouvez générer une activité google map à partir du studio Android.