代码之家  ›  专栏  ›  技术社区  ›  Fahim

将谷歌地图添加到已有项目中

  •  1
  • Fahim  · 技术社区  · 6 年前

    我想展示一个关于现有应用程序的活动地图。我将映射片段添加到xml并获取api密钥,但下面的应用程序出错。我该怎么解决?

    我在activity包上右键单击添加map xml和activity,并选择googlemapsactivity,但仍然不起作用并且有错误。

    我的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:context="com.co.KITIL.MapActivity"/>
    
    <Button
        android:id="@+id/saveBtn"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:text="شروع کار"
        android:textSize="15sp"
        android:background="@color/blue"
        android:textColor="@android:color/white"/>
    </LinearLayout>
    

    我的活动:

    package com.co.KITIL;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.FragmentActivity;
    
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    
    
    public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
    
    SupportMapFragment mapView;
    GoogleMap googleMap;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mapView=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        mapView.getMapAsync(this);
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
    
    }
    }
    

    AndroidManifest:

    <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIz-------------------------bcI" />
    

    google_maps_api.xml from debug.res.values;

    <resources>
    
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AI-----------------bcI</string>
    

    错误:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com..KITIL/com..KITIL.MapsActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                       at a
                                                    Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                                                    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                                                    Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: calling Fragment constructor caused an exception
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Fahim    6 年前

    我在下面的链接中找到了解决方案:

    Google map not showing in android activity?

    我应该启用google maps api,然后再次下载google-services.json并复制到解决方案中。