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

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

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

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

Android

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1992閲覧

実機に位置情報を表示したい

yu9718s

総合スコア9

GPS

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

Android

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

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/06/26 06:40

編集2020/06/26 07:02

前提・実現したいこと

実機で位置情報が表示されるようにしたいです。

発生している問題・エラーメッセージ

エミュレータでは位置情報が表示されるのですが、
実機で実行すると位置情報が表示されません。

該当のソースコード

MainActivity

1import androidx.appcompat.app.AppCompatActivity 2import android.os.Bundle 3import android.Manifest 4import android.location.LocationManager 5import android.content.pm.PackageManager 6import android.content.Context 7import android.content.Intent 8import android.location.Location 9import android.location.LocationListener 10import android.widget.TextView 11import android.provider.Settings 12import android.widget.Toast 13import android.util.Log 14import androidx.core.app.ActivityCompat 15import androidx.core.content.ContextCompat 16 17 18class MainActivity : AppCompatActivity(), LocationListener { 19 20 // lateinit: late initialize to avoid checking null 21 private lateinit var locationManager: LocationManager 22 23 override fun onCreate(savedInstanceState: Bundle?) { 24 super.onCreate(savedInstanceState) 25 setContentView(R.layout.activity_main) 26 27 if (ContextCompat.checkSelfPermission(this, 28 Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 29 ActivityCompat.requestPermissions(this, 30 arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 31 1000) 32 } else { 33 locationStart() 34 35 if (::locationManager.isInitialized) { 36 locationManager.requestLocationUpdates( 37 LocationManager.GPS_PROVIDER, 38 1000, 39 50f, 40 this) 41 } 42 43 } 44 } 45 46 private fun locationStart() { 47 Log.d("debug", "locationStart()") 48 49 // Instances of LocationManager class must be obtained using Context.getSystemService(Class) 50 locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager 51 52 val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager 53 54 if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 55 Log.d("debug", "location manager Enabled") 56 } else { 57 // to prompt setting up GPS 58 val settingsIntent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS) 59 startActivity(settingsIntent) 60 Log.d("debug", "not gpsEnable, startActivity") 61 } 62 63 if (ContextCompat.checkSelfPermission(this, 64 Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 65 ActivityCompat.requestPermissions(this, 66 arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1000) 67 68 Log.d("debug", "checkSelfPermission false") 69 return 70 } 71 72 locationManager.requestLocationUpdates( 73 LocationManager.GPS_PROVIDER, 74 1000, 75 50f, 76 this) 77 78 } 79 80 override fun onRequestPermissionsResult( 81 requestCode: Int, permissions: Array<String>, grantResults: IntArray) { 82 if (requestCode == 1000) { 83 // 使用が許可された 84 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 85 Log.d("debug", "checkSelfPermission true") 86 87 locationStart() 88 89 } else { 90 // それでも拒否された時の対応 91 val toast = Toast.makeText(this, 92 "これ以上なにもできません", Toast.LENGTH_SHORT) 93 toast.show() 94 } 95 } 96 } 97 98 override fun onStatusChanged(provider: String, status: Int, extras: Bundle) { 99 100 } 101 102 override fun onLocationChanged(location: Location) { 103 // Latitude 104 val textView1 = findViewById<TextView>(R.id.text_view1) 105 val str1 = "Latitude:" + location.getLatitude() 106 textView1.text = str1 107 108 // Longitude 109 val textView2 = findViewById<TextView>(R.id.text_view2) 110 val str2 = "Longtude:" + location.getLongitude() 111 textView2.text = str2 112 } 113 114 override fun onProviderEnabled(provider: String) { 115 116 } 117 118 override fun onProviderDisabled(provider: String) { 119 120 } 121 122}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 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 android:orientation="vertical" 7 android:padding="20dp" 8 tools:context=".MainActivity"> 9 10 <TextView 11 android:id="@+id/text_view1" 12 android:textSize="20sp" 13 android:layout_margin="20dp" 14 android:textColor="#44f" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" /> 17 18 <TextView 19 android:id="@+id/text_view2" 20 android:textSize="20sp" 21 android:textColor="#f44" 22 android:layout_margin="20dp" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" /> 25 26</LinearLayout>

Manifest

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="jp.co.stv_tech.test_gps"> 4 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 5 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 6 7 <application 8 android:allowBackup="true" 9 android:icon="@mipmap/ic_launcher" 10 android:label="@string/app_name" 11 android:roundIcon="@mipmap/ic_launcher_round" 12 android:supportsRtl="true" 13 android:theme="@style/AppTheme"> 14 <activity android:name=".MainActivity"> 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 </application> 22 23</manifest>

試したこと

何を試したらよいかすらわからず、調べてみたのですが、
解決に至る情報は見当たりませんでした。
実機では表示されない原因を解決するためにはどんな文言で調べていくべきでしょうか?
ちなみにkotlin GPS 実機 エミュレータ 等で検索しておりました。

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

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

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

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

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

YT0014

2020/06/26 07:42

実機でのログ情報を確認して、問題の切り分けをするべきではないでしょうか?
yu9718s

2020/06/26 08:30

承知いたしました。 正直代々的にエラーが出た時にしかログに着目することなかったので、これからログについても勉強してみます。
keicha_hrs

2020/06/26 14:05

端末がGPSの電波を確実に受信できると思われる屋外に持ち出しても何も表示されないのでしょうか?私の手持ちの端末(Huawei Honor 8, Android 7.0)では正常に動作するようです。
yu9718s

2020/06/29 02:01 編集

仰る通りでした。 窓際で試してみたところ、 正常に動作いたしました。 ありがとうございます。
keicha_hrs

2020/07/01 09:38

無事解決できたのなら良かったです。できましたら、ご自身で回答欄に解決に至った内容(一言二言で結構です)を記した上でそれをベストアンサーとし、質問を解決状態にしてください。
yu9718s

2020/07/01 23:55

承知いたしました。 ご丁寧にありがとうございます。
guest

回答1

0

自己解決

端末を窓際に持ち出すと正常に動作いたしました。

投稿2020/07/01 23:57

yu9718s

総合スコア9

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問