Google地图无法在Android应用中呈现

时间:2017-11-27 01:18:56

标签: android google-maps-android-api-2 google-maps-api-2 android-maps-v2

地图会在下面的屏幕截图中显示位置名称,当我点击获取路线时,我的代码会将我带到正确的位置,但实际的地图不会渲染。当我滚动它有时会尝试渲染,但没有真正的成功。我查看了与此问题相关的每篇文章,但没有一项建议有效。

  • 我的API密钥很好
  • 已启用Google MAPS API
  • 添加依赖项

我一直试图解决这个问题几周但没有成功。我认为它可能是一个模拟器问题,但我使用了几个不同的模拟器,包括Nexus 5.如果有帮助,我也使用Android Studio。

以下是截图:

Maps not rendering

Maps after button pressed

这是我的代码:

Directions.java

 import android.graphics.Color;
    import android.os.Bundle;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    import com.akexorcist.googledirection.DirectionCallback;
    import com.akexorcist.googledirection.GoogleDirection;
    import com.akexorcist.googledirection.constant.TransportMode;
    import com.akexorcist.googledirection.model.Direction;
    import com.akexorcist.googledirection.model.Route;
    import com.akexorcist.googledirection.util.DirectionConverter;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.LatLngBounds;
    import com.google.android.gms.maps.model.MarkerOptions;

    import java.util.ArrayList;

    public class Directions extends AppCompatActivity implements OnMapReadyCallback,
                                    View.OnClickListener, DirectionCallback
    {
        private GoogleMap googleMap;
        private LatLng origin = new LatLng(29.572813, -97.984900);
        private SupportMapFragment mapFragment;
        private LatLng destination;
        private Button get_directions_btn;
        private String serverKey = "AIzaSyCo9g8UvuiWqrksnjTbFKD13wBdUG_BzDI";

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


            origin = new LatLng(29.572813, -97.984900);

            Bundle bundle = getIntent().getExtras();
            double destLatitude = bundle.getDouble("latitude");
            double destLongitude = bundle.getDouble("longitude");
            Log.d("Latitude", "Latitude is: " + destLatitude);
            Log.d("Longitude", "Longitude is:" + destLongitude);
            destination = new LatLng(destLatitude, destLongitude);

            get_directions_btn = (Button) findViewById(R.id.get_directions_btn);
            get_directions_btn.setOnClickListener(this);

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

        }

        public void onMapReady(GoogleMap googleMap) {
            this.googleMap = googleMap;
            if (googleMap == null)
                Toast.makeText(this, "Maps is null", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(this, "Maps is NOT null", Toast.LENGTH_SHORT).show();
        }

        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.get_directions_btn) {
                requestDirection();

            }
        }

        public void requestDirection() {
            Log.d("RequestOrigin", "Origin inside request: " + origin);
            Log.d("RequestDestination", "Destination inside request: " + destination);
            Snackbar.make(get_directions_btn, "Direction Requesting...", Snackbar.LENGTH_SHORT).show();
            GoogleDirection.withServerKey(serverKey)
                    .from(origin)
                    .to(destination)
                    .transportMode(TransportMode.DRIVING)
                    .execute(this);
        }

        public void onDirectionSuccess(Direction direction, String rawBody) {
            Log.d("Status","Direction Status is:" +direction.getStatus());
            Snackbar.make(get_directions_btn, "Success with status : " + direction.getStatus(), Snackbar.LENGTH_SHORT).show();
            if (direction.isOK()) {
                Route route = direction.getRouteList().get(0);
                Log.d("Route ", "Route is: " + route);

                Log.d("Origin Marker", "Origin Marker: " + origin);
                Log.d("Destination Marker", "Destination Marker: " + destination);
                googleMap.addMarker(new MarkerOptions().position(origin));
                googleMap.addMarker(new MarkerOptions().position(destination));

                ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
                googleMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.RED));
                setCameraWithCoordinationBounds(route);

                get_directions_btn.setVisibility(View.GONE);

            }
            else{
                Log.d("Status","Direction Status is:" +direction.getStatus());
                Snackbar.make(get_directions_btn, direction.getStatus(), Snackbar.LENGTH_SHORT).show();
            }
        }

            public void onDirectionFailure(Throwable t) {
                Snackbar.make(get_directions_btn, t.getMessage(), Snackbar.LENGTH_SHORT).show();
            }


        private void setCameraWithCoordinationBounds(Route route) {
            LatLng southwest = route.getBound().getSouthwestCoordination().getCoordination();
            LatLng northeast = route.getBound().getNortheastCoordination().getCoordination();
            LatLngBounds bounds = new LatLngBounds(southwest, northeast);
            googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
        }
    }

XML布局:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_directions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="<package>.Directions">

        <fragment
            android:id="@+id/map_fragment"
            android:name = "com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <Button
            android:id="@+id/get_directions_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Get Directions" />
    </RelativeLayout>

清单:

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

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.READ_PROFILE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".RegisterActivity">
        </activity>
        <activity android:name=".HomeScreen">
        </activity>
        <activity android:name=".Food_Drink">
        </activity>
        <activity android:name=".Entertainment">
        </activity>
        <activity android:name=".Outdoors">
        </activity>
        <activity android:name=".Shopping">
        </activity>
        <activity android:name=".SavedLocations">
        </activity>
        <activity android:name=".SuggestLocation">
        </activity>
        <activity android:name=".Directions">
        </activity>
        <activity android:name=".Instructions">
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key"/>
    </application>

</manifest>

Gradle:

 apply plugin: 'com.android.application'

    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "<app id>"
            minSdkVersion 19
            targetSdkVersion 27
            multiDexEnabled true
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })

        compile 'com.android.support:appcompat-v7:27.0.1'
        compile 'com.android.support:design:27.0.1'
        compile 'com.android.support:recyclerview-v7:27.0.1'
        compile 'com.akexorcist:googledirectionlibrary:1.1.0'
        compile 'com.google.android.gms:play-services-maps:11.6.0'

        testCompile 'junit:junit:4.12'
    }

logcat的

11-26 19:33:30.510 4154-4154/? I/art: Not late-enabling -Xcheck:jni (already on)
11-26 19:33:30.511 4154-4154/? W/art: Unexpected CPU variant for X86 using defaults: x86
11-26 19:33:30.551 4154-4154/? W/System: ClassLoader referenced unknown path: /data/app/sheldonjohn.com.offcampus-1/lib/x86
11-26 19:33:30.583 4154-4154/? I/MultiDex: VM with version 2.1.0 has multidex support
11-26 19:33:30.583 4154-4154/? I/MultiDex: Installing application
11-26 19:33:30.583 4154-4154/? I/MultiDex: VM has multidex support, MultiDex support library is disabled.
11-26 19:33:31.771 4154-4161/? I/art: Debugger is no longer active
11-26 19:33:31.771 4154-4161/? I/art: Starting a blocking GC Instrumentation
11-26 19:33:33.041 4154-4154/sheldonjohn.com.offcampus W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-26 19:33:33.169 4154-4154/sheldonjohn.com.offcampus I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-26 19:33:33.177 4154-4154/sheldonjohn.com.offcampus I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
11-26 19:33:33.300 4154-4187/sheldonjohn.com.offcampus I/OpenGLRenderer: Initialized EGL, version 1.4
11-26 19:33:33.300 4154-4187/sheldonjohn.com.offcampus D/OpenGLRenderer: Swap behavior 1
11-26 19:33:33.303 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglCreateContext: 0xb4385060: maj 2 min 0 rcv 2
11-26 19:33:33.333 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:33.390 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:49.478 4154-4154/sheldonjohn.com.offcampus W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
11-26 19:33:55.329 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:55.411 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:55.435 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:55.488 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:55.510 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:55.552 4154-4154/sheldonjohn.com.offcampus W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
11-26 19:33:56.554 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:56.707 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:33:56.730 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:34:00.078 4154-4154/sheldonjohn.com.offcampus I/zzbz: Making Creator dynamically
11-26 19:34:00.086 4154-4154/sheldonjohn.com.offcampus I/DynamiteModule: Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:19
11-26 19:34:00.086 4154-4154/sheldonjohn.com.offcampus I/DynamiteModule: Selected remote version of com.google.android.gms.maps_dynamite, version >= 19
11-26 19:34:00.094 4154-4154/sheldonjohn.com.offcampus W/System: ClassLoader referenced unknown path: 
11-26 19:34:00.094 4154-4154/sheldonjohn.com.offcampus W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
11-26 19:34:00.118 4154-4154/sheldonjohn.com.offcampus I/Google Maps Android API: Google Play services client version: 11717000
11-26 19:34:00.121 4154-4154/sheldonjohn.com.offcampus I/Google Maps Android API: Google Play services package version: 11743470
11-26 19:34:00.299 4154-4205/sheldonjohn.com.offcampus D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-26 19:34:00.359 4154-4154/sheldonjohn.com.offcampus D/Latitude: Latitude is: 29.584699
11-26 19:34:00.359 4154-4154/sheldonjohn.com.offcampus D/Longitude: Longitude is:-97.990383

                                                                    [ 11-26 19:34:00.475  4154: 4227 D/         ]
                                                                    HostConnection::get() New Host Connection established 0xb43889c0, tid 4227


                                                                    [ 11-26 19:34:00.476  4154: 4227 W/         ]
                                                                    Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 
11-26 19:34:00.480 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:34:00.482 4154-4227/sheldonjohn.com.offcampus D/EGL_emulation: eglCreateContext: 0xb4386020: maj 1 min 0 rcv 1
11-26 19:34:00.509 4154-4165/sheldonjohn.com.offcampus I/art: Background partial concurrent mark sweep GC freed 16914(1528KB) AllocSpace objects, 3(124KB) LOS objects, 33% free, 7MB/11MB, paused 5.759ms total 27.888ms
11-26 19:34:00.518 4154-4227/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4386020: ver 1 0 (tinfo 0x977679c0)
11-26 19:34:00.638 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:34:00.757 4154-4187/sheldonjohn.com.offcampus D/EGL_emulation: eglMakeCurrent: 0xb4385060: ver 2 0 (tinfo 0xb43831d0)
11-26 19:34:02.362 4154-4229/sheldonjohn.com.offcampus W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
11-26 19:34:02.366 4154-4229/sheldonjohn.com.offcampus I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
11-26 19:34:02.366 4154-4229/sheldonjohn.com.offcampus I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
11-26 19:34:02.368 4154-4229/sheldonjohn.com.offcampus W/System: ClassLoader referenced unknown path: /data/user_de/0/com.google.android.gms/app_chimera/m/00000001/n/x86

0 个答案:

没有答案
相关问题