質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
GPS

GPSは、Global Positioning Systemの略です。衛星信号を使用して受信機の地上又は空中内の居場所を特定するナビゲーションシステムです。"GPS"は受信機のことも指します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

パーミッション

パーミッション(許可)は、ファイルシステム、データベース、そして他のコンピュータシステムに対するユーザーのアクセス権のことを指します。

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

Q&A

0回答

897閲覧

Androidアプリで,GPSのパーミッション承認のリクエストを送りたい!

SyuheiFujita

総合スコア18

GPS

GPSは、Global Positioning Systemの略です。衛星信号を使用して受信機の地上又は空中内の居場所を特定するナビゲーションシステムです。"GPS"は受信機のことも指します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

パーミッション

パーミッション(許可)は、ファイルシステム、データベース、そして他のコンピュータシステムに対するユーザーのアクセス権のことを指します。

Google マップ

Google Mapは、Google社がオンラインで提供している地図・ローカル検索サービスです。GIS(Geographic Information System:地理情報システム)の中の「WebGIS」に該当します。地図・航空写真・地形の表示方式があり、それぞれユーザーが縮尺を調整して表示させることができます。地域の情報サービスを検索する機能やルート検索の機能も搭載されています。

0グッド

0クリップ

投稿2019/12/25 02:21

編集2019/12/26 12:22

Androidアプリ開発についての質問です!
現在,GPSを使って位置データを取得して,GoogleMapに表示させるAndroidアプリを作りたい!と思っています.

GPSのパーミッションを承認するリクエストを送る処理をする際にエラーが出てしまうのですが(おそらく),方針としては以下のようなコードであっていますでしょうか?
よろしければご教授していただきたいです!

MapsActivity

1package com.example.googlemap_gps_location_android 2 3import android.Manifest 4import android.content.pm.PackageManager 5import android.location.Location 6import android.location.LocationListener 7import android.os.Bundle 8import androidx.appcompat.app.AppCompatActivity 9import androidx.core.app.ActivityCompat 10import androidx.core.content.ContextCompat 11import com.google.android.gms.maps.GoogleMap 12import com.google.android.gms.maps.OnMapReadyCallback 13import com.google.android.gms.maps.SupportMapFragment 14 15const val MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 0 16 17class MapsActivity : AppCompatActivity(), OnMapReadyCallback, LocationListener{ 18 19 private lateinit var mMap: GoogleMap 20 21 override fun onCreate(savedInstanceState: Bundle?) { 22 super.onCreate(savedInstanceState) 23 setContentView(R.layout.activity_maps) 24 // Obtain the SupportMapFragment and get notified when the map is ready to be used. 25 26 val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment 27 mapFragment.getMapAsync(this) 28 29 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 30 != PackageManager.PERMISSION_GRANTED) { 31 // Permission is not granted 32 checkPermission() 33 } 34 } 35 36 override fun onMapReady(googleMap: GoogleMap) { 37 mMap = googleMap 38 39 mMap.isMyLocationEnabled = true 40 } 41 42 override fun onRequestPermissionsResult( 43 requestCode: Int, 44 permissions: Array<String>, 45 grantResults: IntArray 46 ) { 47 if (requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) { 48 // If request is cancelled, the result arrays are empty. 49 if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { 50 // permission was granted, yay! Do the 51 // contacts-related task you need to do. 52 53 } else { 54 // permission denied, boo! Disable the 55 // functionality that depends on this permission. 56 } 57 } 58 59 // Add other 'when' lines to check for other 60 // permissions this app might request. 61 } 62 63 override fun onLocationChanged(location: Location) { 64 } 65 66 override fun onProviderEnabled(provider: String) { 67 } 68 69 override fun onProviderDisabled(provider: String) { 70 } 71 72 override fun onStatusChanged(provider: String, status: Int, extras: Bundle) { 73 } 74 75 private fun checkPermission() { 76 // Here, thisActivity is the current activity 77 if (ContextCompat.checkSelfPermission(this, 78 Manifest.permission.ACCESS_FINE_LOCATION) 79 != PackageManager.PERMISSION_GRANTED) { 80 81 // Permission is not granted 82 // Should we show an explanation? 83 if (ActivityCompat.shouldShowRequestPermissionRationale(this, 84 Manifest.permission.ACCESS_FINE_LOCATION)) { 85 // Show an explanation to the user *asynchronously* -- don't block 86 // this thread waiting for the user's response! After the user 87 // sees the explanation, try again to request the permission. 88 89 } else { 90 // No explanation needed, we can request the permission. 91 ActivityCompat.requestPermissions(this, 92 arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 93 MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) 94 95 // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 96 // app-defined int constant. The callback method gets the 97 // result of the request. 98 } 99 } else { 100 101 } 102 } 103} 104

AndroidManifest

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.googlemap_gps_location_android"> 4 <!-- 5 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 6 Google Maps Android API v2, but you must specify either coarse or fine 7 location permissions for the 'MyLocation' functionality. 8 --> 9 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 10 11 <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" /> 12 13 <application 14 android:allowBackup="true" 15 android:icon="@mipmap/ic_launcher" 16 android:label="@string/app_name" 17 android:roundIcon="@mipmap/ic_launcher_round" 18 android:supportsRtl="true" 19 android:theme="@style/AppTheme"> 20 <activity 21 android:name=".MainActivity" 22 android:label="@string/title_activity_main" 23 android:theme="@style/AppTheme.NoActionBar"> 24 25 </activity> 26 <!-- 27 The API key for Google Maps-based APIs is defined as a string resource. 28 (See the file "res/values/google_maps_api.xml"). 29 Note that the API key is linked to the encryption key used to sign the APK. 30 You need a different API key for each encryption key, including the release key that is used to 31 sign the APK for publishing. 32 You can define the keys for the debug and release targets in src/debug/ and src/release/. 33 --> 34 <meta-data 35 android:name="com.google.android.geo.API_KEY" 36 android:value="@string/google_maps_key" /> 37 38 <activity 39 android:name=".MapsActivity" 40 android:label="GoogleMap-GPS-Location"> 41 <intent-filter> 42 <action android:name="android.intent.action.MAIN" /> 43 44 <category android:name="android.intent.category.LAUNCHER" /> 45 </intent-filter> 46 </activity> 47 </application> 48 49</manifest>

error

12019-12-25 11:17:44.033 23317-23317/com.example.googlemap_gps_location_android E/AndroidRuntime: FATAL EXCEPTION: main 2 Process: com.example.googlemap_gps_location_android, PID: 23317 3 java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION 4 at com.google.maps.api.android.lib6.impl.bm.c(:com.google.android.gms.dynamite_mapsdynamite@19831084@19.8.31 (120700-0):771) 5 at com.google.android.gms.maps.internal.j.a(:com.google.android.gms.dynamite_mapsdynamite@19831084@19.8.31 (120700-0):273) 6 at ck.onTransact(:com.google.android.gms.dynamite_mapsdynamite@19831084@19.8.31 (120700-0):5) 7 at android.os.Binder.transact(Binder.java:914) 8 at com.google.android.gms.internal.maps.zza.zzb(Unknown Source:20) 9 at com.google.android.gms.maps.internal.zzg.setMyLocationEnabled(Unknown Source:109) 10 at com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(Unknown Source:112) 11 at com.example.googlemap_gps_location_android.MapsActivity.onMapReady(MapsActivity.kt:39) 12 at com.google.android.gms.maps.zzak.zza(Unknown Source:2) 13 at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source:12) 14 at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source:12) 15 at android.os.Binder.transact(Binder.java:914) 16 at cj.b(:com.google.android.gms.dynamite_mapsdynamite@19831084@19.8.31 (120700-0):14) 17 at com.google.android.gms.maps.internal.bb.a(:com.google.android.gms.dynamite_mapsdynamite@19831084@19.8.31 (120700-0):4) 18 at com.google.maps.api.android.lib6.impl.bi.run(:com.google.android.gms.dynamite_mapsdynamite@19831084@19.8.31 (120700-0):2) 19 at android.os.Handler.handleCallback(Handler.java:883) 20 at android.os.Handler.dispatchMessage(Handler.java:100) 21 at android.os.Looper.loop(Looper.java:214) 22 at android.app.ActivityThread.main(ActivityThread.java:7356) 23 at java.lang.reflect.Method.invoke(Native Method) 24 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 25 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

※ちなみに,手動でパーミッションを承認すると正常に動作することが確認できています
イメージ説明 イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問