2016-07-30 17 views
0

Ich habe alles getan, indem Sie ein Tutorial folgen, die Karte wird im Tutorial angezeigt, aber es wird nicht in meinem angezeigt, auch wenn ich genau dasselbe getan habe.Android: Google Maps wird nicht angezeigt

MainActivity.Java

package com.example.ankit.mrestro; 

import android.app.Dialog; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapView; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
    private static final int GPS_ERRORDIALOGUE_REQUEST = 9001; 
    GoogleMap mMap; 
    MapView mMapView; 
    Button bLogout; 
    EditText etFName, etLName, etAge, etEmail, etUserName; 
    UserLocalStorage userLocalStorage; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (servicesOK()){ 
      Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show(); 
      setContentView(R.layout.activity_mapview); 

      mMapView = (MapView) findViewById(R.id.map); 
      mMapView.onCreate(savedInstanceState); 


     } 
     setContentView(R.layout.activity_main); 

     etFName = (EditText) findViewById(R.id.etFName); 
     etLName = (EditText) findViewById(R.id.etLName); 
     etAge = (EditText) findViewById(R.id.etAge); 
     etEmail = (EditText) findViewById(R.id.etEmail); 
     etUserName = (EditText) findViewById(R.id.etUserName); 

     bLogout = (Button) findViewById(R.id.bLogout); 
     bLogout.setOnClickListener(this); 
     userLocalStorage = new UserLocalStorage(this); 


     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     if (authenticate() == true) { 
      DisplaysUserDetails(); 
     } 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.ankit.mrestro/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    private boolean authenticate() { 
     return userLocalStorage.getUserLoggedIn(); 

    } 

    private void DisplaysUserDetails() { 
     User user = userLocalStorage.GetLoggedInUser(); 
     etUserName.setText(user.UserName); 
     etFName.setText(user.FirstName); 
     etLName.setText(user.LastName); 
     etAge.setText(user.Age + ""); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.bLogout: 
       userLocalStorage.ClearUserData(); 
       userLocalStorage.SetUserLoggedIn(false); 
       startActivity(new Intent(this, Login.class)); 
       break; 
     } 
    } 

    public boolean servicesOK() { 
     int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (isAvailable == ConnectionResult.SUCCESS) { 
      return true; 
     } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) { 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOGUE_REQUEST); 
      dialog.show(); 
     } 
     else { 
      Toast.makeText(this, "Can't connect to google play services", Toast.LENGTH_SHORT).show(); 

     } 
     return false; 


    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     mMapView.onDestroy(); 

    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mMapView.onLowMemory(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     mMapView.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mMapView.onResume(); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     mMapView.onSaveInstanceState(outState); 

    } 



    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction2 = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://com.example.ankit.mrestro/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction2); 
     client.disconnect(); 
    } 
} 

AndroidManifest

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

    <uses-sdk 
     android:minSdkVersion="15" 
     android:targetSdkVersion="23"/> 

     <permission android:name="com.example.ankit.mrestro.permission.MAPS_RECEIVE" 
      android:protectionLevel="signature"/> 

     <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsg.permission.READ_GSERVICES"/> 

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-feature android:glEsVersion="0x00020000" 
     android:required="true"/> 


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

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="<<APIKEY>>"/> 




     <activity 
      android:name=".Login" 
      android:label="@string/title_activity_login" 
      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=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".Register" 
      android:label="@string/title_activity_register" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".Main2Activity" 
      android:label="@string/title_activity_main2" 
      android:theme="@style/AppTheme.NoActionBar"></activity> 
     <!-- ATTENTION: This was auto-generated to add Google Play services to your project for 
      App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
    </application> 

</manifest> 

activity_mapview

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

</LinearLayout> 

I‘ Ich habe bemerkt, dass ich keine separate Schaltfläche zum Anzeigen der Karte erstellt habe. Könnte das das Problem sein? Wenn ja, wie weise ich Google Maps eine Schaltfläche zu? Ich weiß, wie man eine Schaltfläche erstellt und ihr eine Aktivität zuweist, aber wie mache ich das für Maps? Bitte helfen Sie.

+0

Mögliches Duplikat von [Android: Google Maps wird nicht angezeigt] (http://stackoverflow.com/questions/16551458/android-google-maps-not-displaying) –

Antwort

0

Wo bekommen Sie Ihre Karte? Sie sollten in Ihrer Aktivität implementieren OnMapReadyCallback und dann in onCreate wie folgt vor:

 if (/* play services are OK */) { 
     mMapView.onCreate(savedInstanceState); 
     mMapView.getMapAsync(this); 
    } 

Und dann ein onMapReady Anruf (so ähnlich) außer Kraft setzen:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mGoogleMap = googleMap; 
    UiSettings uiSettings = mGoogleMap.getUiSettings(); 
    uiSettings.setZoomControlsEnabled(true); 

    if (mCameraPosition != null) { 
     mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition)); 
    } 
    addMarkers(); 
} 

Sie können meinen Code überprüfen: https://github.com/dmytroKarataiev/EarthquakeSurvival/blob/master/app/src/main/java/com/adkdevelopment/earthquakesurvival/MapviewFragment.java

Auch sehr wichtig: Zeigen Sie NIEMALS jemand Ihren API-Schlüssel. Es verstößt gegen die Google-Regeln und ist auch nicht sicher für Sie.

Verwandte Themen