前提・実現したいこと
GoogleMapAPIを利用したアプリを作っています。
そこで、GoogleMapをFragmentで表示したいのですが、見た感じAPIは読み込まれているはずなのにマップが表示されません。
発生している問題
AndroidStudioで新規プロジェクトを作るときに標準で用意されている「Googleマップ・アクティビティー」からソースコードはほぼ改変なしでコピペしましたが、左下にGoogleのロゴは出るものの、マップが出ません。(画像添付)
該当のソースコード
MainActivity.kt
kotlin
1package com.example.hogehoge 2 3import android.content.Intent 4import android.os.Bundle 5import android.widget.TextView 6import androidx.appcompat.app.AppCompatActivity 7import com.example.otukai_watch.ToDoList.ItemActivity 8import com.example.otukai_watch.VoiceChat.VoiceActivity 9import com.google.android.material.bottomnavigation.BottomNavigationView 10 11import kotlinx.android.synthetic.main.activity_main.* 12 13 14class MainActivity : AppCompatActivity() { 15 16 private lateinit var textMessage: TextView 17 private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> 18 when (item.itemId) { 19 R.id.navigation_home -> { 20 viewflipper.displayedChild=0 21 return@OnNavigationItemSelectedListener true 22 } 23 R.id.navigation_dashboard -> { 24 val intent = Intent(this, VoiceActivity::class.java) 25 startActivity(intent) 26 } 27 R.id.navigation_notifications -> { 28 val intent = Intent(this, ItemActivity::class.java) 29 startActivity(intent) 30 } 31 } 32 false 33 } 34 35 override fun onCreate(savedInstanceState: Bundle?) { 36 super.onCreate(savedInstanceState) 37 setContentView(R.layout.activity_main) 38 val navView: BottomNavigationView = findViewById(R.id.nav_view) 39 40 navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener) 41 } 42 43}
MapFragment.kt
kotlin
1package com.example.hogehoge 2 3import android.os.Bundle 4import android.view.LayoutInflater 5import android.view.View 6import android.view.ViewGroup 7import androidx.appcompat.app.AppCompatActivity 8import androidx.fragment.app.Fragment 9import com.google.android.gms.maps.CameraUpdateFactory 10import com.google.android.gms.maps.GoogleMap 11import com.google.android.gms.maps.OnMapReadyCallback 12import com.google.android.gms.maps.SupportMapFragment 13import com.google.android.gms.maps.model.LatLng 14import com.google.android.gms.maps.model.MarkerOptions 15 16class mapFragment : AppCompatActivity(), OnMapReadyCallback { 17 18 private lateinit var mMap: GoogleMap 19 20 override fun onCreate(savedInstanceState: Bundle?) { 21 super.onCreate(savedInstanceState) 22 setContentView(R.layout.fragment_map) 23 // Obtain the SupportMapFragment and get notified when the map is ready to be used. 24 val mapFragment = supportFragmentManager 25 .findFragmentById(R.id.map) as SupportMapFragment 26 mapFragment.getMapAsync(this) 27 } 28 29 30 override fun onMapReady(googleMap: GoogleMap) { 31 mMap = googleMap 32 33 // Add a marker in Sydney and move the camera 34 val sydney = LatLng(-34.0, 151.0) 35 mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney")) 36 mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)) 37 } 38}
activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:id="@+id/container" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <com.google.android.material.bottomnavigation.BottomNavigationView 12 android:id="@+id/nav_view" 13 android:layout_width="0dp" 14 android:layout_height="wrap_content" 15 android:layout_marginEnd="0dp" 16 android:layout_marginStart="0dp" 17 android:background="?android:attr/windowBackground" 18 app:layout_constraintBottom_toBottomOf="parent" 19 app:layout_constraintLeft_toLeftOf="parent" 20 app:layout_constraintRight_toRightOf="parent" 21 app:menu="@menu/bottom_nav_menu"/> 22 <ViewFlipper 23 android:id="@+id/viewflipper" 24 25 android:layout_width="match_parent" 26 android:layout_height="0dp" 27 app:layout_constraintBottom_toTopOf="@+id/nav_view" 28 app:layout_constraintTop_toTopOf="parent" 29 app:layout_constraintEnd_toEndOf="parent" 30 app:layout_constraintStart_toStartOf="parent" 31 32 > 33 <include layout="@layout/activity_maps1"/> 34 </ViewFlipper> 35 36 37</androidx.constraintlayout.widget.ConstraintLayout> 38 39
fragment_map.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity"> 7 8 <fragment 9 android:name="com.google.android.gms.maps.SupportMapFragment" 10 android:id="@+id/frg" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent"/> 13 14</FrameLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hogehoge"> <!-- 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"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <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"> <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key"/> <activity android:name=".MainActivity" 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> <activity android:name=".ToDoList.ItemActivity" android:label="@string/title_activity_main2" android:theme="@style/AppTheme.NoActionBar"> </activity> <activity android:name=".VoiceChat.VoiceActivity" android:label="@string/title_activity_main2" android:theme="@style/AppTheme.NoActionBar"> </activity> <activity android:name=".VoiceChat.RecordListActivity" android:screenOrientation="portrait" /> </application> </manifest>
補足情報
Gradle プラグインバージョン: 3.4.2
Gradleバージョン: 5.1.1
kotlinバージョン: 1.3.41
minSdkVersion 21
targetSdkVersion 28
Android Studio 3.4.2
Build #AI-183.6156.11.34.5692245, built on June 27, 2019
JRE: 1.8.0_152-release-1343-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
よろしくおねがいします
回答1件
あなたの回答
tips
プレビュー