GoogleMapsAcitivyでの位置情報の取得をしようとしています。
位置情報を取得する際の方法としてMapsActivityにこのコードを記載しています。
import
1import android.content.Context; 2import android.content.pm.PackageManager; 3import android.location.Criteria; 4import android.location.Location; 5import android.location.LocationListener; 6import android.location.LocationManager; 7import android.location.LocationProvider; 8import android.os.Bundle; 9import android.util.Log; 10import android.widget.CompoundButton; 11 12import androidx.annotation.NonNull; 13import androidx.core.app.ActivityCompat; 14import androidx.fragment.app.FragmentActivity; 15 16import com.example.otamesisisisii.R; 17import com.google.android.gms.maps.CameraUpdateFactory; 18import com.google.android.gms.maps.GoogleMap; 19import com.google.android.gms.maps.OnMapReadyCallback; 20import com.google.android.gms.maps.SupportMapFragment; 21import com.google.android.gms.maps.model.LatLng; 22 23public class MapsActivity extends FragmentActivity 24 implements OnMapReadyCallback, LocationListener, 25 CompoundButton.OnCheckedChangeListener { 26 private GoogleMap mMap; 27 private LatLng latlong; 28 private static Location location1; 29 LocationManager locationmanager1; 30 private static final int LOCATION_CODE = 100; 31 private static final String[] LOCATION_PERMISSION = { 32 Manifest.permission.ACCESS_FINE_LOCATION, 33 Manifest.permission.ACCESS_COARSE_LOCATION 34 }; 35 private static final int GRANTED = PackageManager.PERMISSION_GRANTED; 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 // ここではまだsetContentView()は呼ばない 41 42 locationmanager1 = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 43 44 if (ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[0]) != GRANTED && 45 ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[1]) != GRANTED) { 46 // ↓「アクセスを許可しますか?」 47 RequestPermissions(LOCATION_PERMISSION, LOCATION_CODE); 48 } else { 49 startLocation(); 50 } 51 } 52 53 54 @Override 55 public void onMapReady(GoogleMap googleMap) { 56 57 mMap = googleMap; 58 double lat = location1.getLatitude(); 59 double lon = location1.getLongitude(); 60 latlong = new LatLng(lat, lon); 61 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 19)); 62 locationmanager1.removeUpdates(this); 63 } 64 65 // アクセス許可のダイアログで操作を行ったときに呼ばれるメソッド 66 @Override 67 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 68 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 69 if (requestCode == LOCATION_CODE) { 70 if (grantResults[0] == GRANTED) { 71 startLocation(); 72 } else { 73 // 拒否されたのならアプリ続行不可能 74 finish(); 75 } 76 } 77 78 } 79 80 void startLocation() { 81 if (ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[0]) == GRANTED || 82 ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[1]) == GRANTED) { 83 84 // 許可を得られたことを確認できた段階で初めてsetContentView()を呼ぶ 85 // onMapReady()が走るのはこれ以後になる 86 setContentView(R.layout.activity_maps); 87 SupportMapFragment mapFragment = 88 (SupportMapFragment) getSupportFragmentManager() 89 .findFragmentById(R.id.map_goo_fra); 90 mapFragment.getMapAsync(this); 91 92 Criteria criteria = new Criteria(); 93 criteria.setAccuracy(Criteria.ACCURACY_FINE); 94 String provider = locationmanager1.getBestProvider(criteria, true); 95 locationmanager1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 96 location1 = locationmanager1.getLastKnownLocation(provider); 97 } 98 } 99 100 @Override 101 protected void onDestroy() { 102 super.onDestroy(); 103 locationmanager1.removeUpdates(this); 104 } 105 106 @Override 107 public void onStatusChanged(String provider, int status, Bundle extras) { 108 switch (status) { 109 case LocationProvider.AVAILABLE: 110 Log.d("debug", "LocationProvider.AVAILABLE"); 111 break; 112 case LocationProvider.OUT_OF_SERVICE: 113 Log.d("debug", "LocationProvider.OUT_OF_SERVICE"); 114 break; 115 case LocationProvider.TEMPORARILY_UNAVAILABLE: 116 Log.d("debug", "LocationProvider.TEMPORARILY_UNAVAILABLE"); 117 break; 118 } 119 } 120 121 @Override 122 public void onLocationChanged(Location location) { 123 //LatLng curr = new LatLng(location.getLatitude(), location.getLongitude()); 124 //mMap.animateCamera(CameraUpdateFactory.newLatLng(curr)); 125 } 126 127 @Override 128 public void onProviderEnabled(String s) { 129 } 130 131 @Override 132 public void onProviderDisabled(String s) { 133 } 134 135 @Override 136 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 137 138 } 139} 140
ですが、次のようなエラーが出ます。
1.初回のpublic void MapsActivityが線を引かれること
2. // ↓「アクセスを許可しますか?」
RequestPermissions(LOCATION_PERMISSION, LOCATION_CODE);
3. setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map_goo_fra);
この三点の太線のところがエラーが起こってしまうところです。
解消方法を教えてください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/19 08:11
2020/09/19 08:14
2020/09/19 08:34
2020/09/19 08:45 編集
2020/09/19 10:47
2020/09/19 11:30
2020/09/23 10:48