android-fragments 谷歌Map不显示在android活动?

t5zmwmid  于 2022-11-13  发布在  Android
关注(0)|答案(9)|浏览(172)

在我的android应用程序中,我希望有一个谷歌Map占据整个屏幕的MainActivity。
我的问题是谷歌Map不显示Map,只在左下角显示谷歌标志:

以下是应显示Map的主要活动的相关部分:

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

下面是保存google map片段的主活动内容的xml:

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

下面是清单文件:

<?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>
3df52oht

3df52oht1#

你需要从谷歌控制台为你的项目启用谷歌MapAPI。而且你还需要从控制台下载JSON文件。

1.转到google console
2.单击您项目。
3.单击左侧菜单面板中的库部分。请参见下图

1.在谷歌MapAPI中,点击谷歌Map安卓API
1.也不愿启用它。
1.现在后藤firebase console
1.点击你的项目,后藤项目设置,下载json文件。
1.在应用程序中添加json文件。

hi3rlvi2

hi3rlvi22#

我刚查了谷歌Map和你的一样。
把你的代码和我的比较一下。
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));
}

}
和清单

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

注意:你必须在谷歌创建一个项目
click here to visit the google console
替换google_maps_api中的键值

<resources>

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

希望这对你有用。
PS:你可以从android studio生成谷歌Map活动。

wn9m85ua

wn9m85ua3#

将此权限添加到菜单

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

或检查Google控制台是否已启用API
或者检查您的google_api_key

mrwjdhj3

mrwjdhj34#

application文件的application部分中包含以下代码:

<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />

请参阅此处的文档。

kiz8lqtg

kiz8lqtg5#

GoogleMap喜欢获取不同的活动生命周期状态。因此,您应该发送它:map_view.onCreate(savedInstanceState)map_view.onResume()。如果出现onPause()onLowMemory()onSaveInstanceState(outState)onDestroy(),它也会喜欢它们。

lyr7nygr

lyr7nygr6#

点击限制密钥,然后在API限制-〉创建API时不限制密钥

wlp8pajw

wlp8pajw7#

1)请更新Google Play服务
2)请在url中附加您的api密钥
例如:-"https://maps.googleapis.com/maps/api/directions/”+输出+“?”+参数+“&密钥=”+ MY_API_KEY;

mrphzbgm

mrphzbgm8#

请确保您已授予这些权限

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

并检查AVD的WiFi是否打开,以及您的PC是否连接到互联网。
我也遇到了同样的问题,按照上面提到的步骤,我能够解决我的问题。

jchrr9hc

jchrr9hc9#

如果“活动”为“正在打开,”并且即使您提供Google_map_key也看不到Map,然后一个解决方案,为我工作的是,我创建了一个新的谷歌MapAPI KEY通过复制链接在google_maps_api.xml和粘贴在浏览器中.选择一个项目或创建一个项目,它会降落在页面上,谷歌API KEY是准备好了复制它,并粘贴在地方给它的指示.它会起作用。

相关问题