首页 > 解决方案 > Android Studio 地图不显示

问题描述

由于冠状病毒的争论,我在家,所以我正在努力推进我的学业。现在我在创建 Maps APP 时遇到了 Android Studio 问题。APP需要通过纬度和经度找到一个位置,也需要一个城市的名称,并通过一个浮动操作按钮定位您的位置。该代码运行良好,当我添加代码以定位您的位置时,地图不再显示。它加载没有错误,但地图不起作用。

地图捕获

显现。

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

    <!--
         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.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <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="AIzaSyAHwtaI8O698ZSeVM8Y4xvjRXxAYi9Kr38" />
        <uses-library android:name="com.google.android.maps" />

        <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>

</manifest>

还要附加依赖项。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.android.support:design:29.0.0'
}

标签: javaandroidandroid-studiomapsmanifest

解决方案


我添加了这个,当我单击按钮时,找到我的位置并移动到它。

 FloatingActionButton myUBICATION = findViewById(R.id.myUBICATION);
        myUBICATION.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mMap != null){
                    mMap.setOnMyLocationClickListener(new GoogleMap.OnMyLocationClickListener() {
                        @Override
                        public void onMyLocationClick(@NonNull Location location) {

                            LatLng GPS = new LatLng(location.getLatitude(), location.getLongitude());
                            mMap.addMarker(new MarkerOptions().position(GPS).title("Marker my ubication"));
                            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(GPS, 10));
                        }
                    });
                }

            }
        });

推荐阅读