また、現在地はgoogleの本社が出てしまっていてインターネットからの位置情報しか取れていません。
JAVA
1コードpackage com.example.myapplication; 2 3import androidx.annotation.NonNull; 4import androidx.core.app.ActivityCompat; 5import androidx.fragment.app.FragmentActivity; 6 7import android.Manifest; 8import android.app.Activity; 9import android.content.Context; 10import android.content.Intent; 11import android.content.pm.PackageManager; 12import android.location.Criteria; 13import android.location.Location; 14import android.location.LocationListener; 15import android.location.LocationManager; 16import android.location.LocationProvider; 17import android.os.Bundle; 18import android.os.IBinder; 19import android.provider.Settings; 20import android.telephony.CarrierConfigManager; 21import android.util.Log; 22import android.view.Menu; 23import android.view.MenuItem; 24import android.widget.CompoundButton; 25import android.widget.TextView; 26import android.widget.Toast; 27 28import com.google.android.gms.maps.CameraUpdate; 29import com.google.android.gms.maps.CameraUpdateFactory; 30import com.google.android.gms.maps.GoogleMap; 31import com.google.android.gms.maps.OnMapReadyCallback; 32import com.google.android.gms.maps.SupportMapFragment; 33import com.google.android.gms.maps.model.LatLng; 34import com.google.android.gms.maps.model.Marker; 35import com.google.android.gms.maps.model.MarkerOptions; 36 37import java.util.Locale; 38 39 40public class MapsActivity extends FragmentActivity 41 implements OnMapReadyCallback, LocationListener, 42 CompoundButton.OnCheckedChangeListener { 43 private GoogleMap mMap; 44 private LatLng location; 45 private LatLng latlong; 46 private static Location location1; 47 LocationManager locationmanager1; 48 private static final int LOCATION_CODE = 100; 49 private static final String[] LOCATION_PERMISSION = { 50 Manifest.permission.ACCESS_COARSE_LOCATION, 51 Manifest.permission.ACCESS_FINE_LOCATION, 52 53 54 }; 55 private static final int GRANTED = PackageManager.PERMISSION_GRANTED; 56 57 @Override 58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 // ここではまだsetContentView()は呼ばない 61 62 locationmanager1 = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 63 64 if (ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[0]) != GRANTED && 65 ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[1]) != GRANTED) { 66 // ↓「アクセスを許可しますか?」 67 ActivityCompat.requestPermissions(this, LOCATION_PERMISSION, LOCATION_CODE); 68 } else { 69 startLocation(); 70 } 71 } 72 73 74 @Override 75 public void onMapReady(GoogleMap googleMap) { 76 77 mMap = googleMap; 78 double lat = location1.getLatitude(); 79 double lon = location1.getLongitude(); 80 latlong = new LatLng(lat, lon); 81 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 19)); 82 83 } 84 85 // アクセス許可のダイアログで操作を行ったときに呼ばれるメソッド 86 @Override 87 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 88 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 89 if (requestCode == LOCATION_CODE) { 90 if (grantResults[0] == GRANTED) { 91 startLocation(); 92 } else { 93 // 拒否されたのならアプリ続行不可能 94 finish(); 95 } 96 } 97 98 } 99 100 void startLocation() { 101 if (ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[0]) == GRANTED || 102 ActivityCompat.checkSelfPermission(this, LOCATION_PERMISSION[1]) == GRANTED) { 103 104 // 許可を得られたことを確認できた段階で初めてsetContentView()を呼ぶ 105 // onMapReady()が走るのはこれ以後になる 106 setContentView(R.layout.activity_maps); 107 SupportMapFragment mapFragment = 108 (SupportMapFragment) getSupportFragmentManager() 109 .findFragmentById(R.id.map); 110 mapFragment.getMapAsync(this); 111 112 Criteria criteria = new Criteria(); 113 criteria.setAccuracy(Criteria.ACCURACY_FINE); 114 String provider = locationmanager1.getBestProvider(criteria, true); 115 locationmanager1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 116 location1 = locationmanager1.getLastKnownLocation(provider); 117 } 118 } 119 120 @Override 121 protected void onDestroy() { 122 super.onDestroy(); 123 locationmanager1.removeUpdates(this); 124 } 125 126 @Override 127 public void onStatusChanged(String provider, int status, Bundle extras) { 128 switch (status) { 129 case LocationProvider.AVAILABLE: 130 Log.d("debug", "LocationProvider.AVAILABLE"); 131 break; 132 case LocationProvider.OUT_OF_SERVICE: 133 Log.d("debug", "LocationProvider.OUT_OF_SERVICE"); 134 break; 135 case LocationProvider.TEMPORARILY_UNAVAILABLE: 136 Log.d("debug", "LocationProvider.TEMPORARILY_UNAVAILABLE"); 137 break; 138 } 139 140 141 } 142 143 @Override 144 public void onLocationChanged(Location location1) { 145 LatLng myLocation = new LatLng(location1.getLatitude(), location1.getLongitude()); 146 mMap.addMarker(new MarkerOptions().position(myLocation).title("now Location")); 147 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 18)); 148 try { 149 locationmanager1.removeUpdates(this); 150 151 } catch(SecurityException e) { 152 // エラー処理 153 } 154 155 //LatLng curr = new LatLng(location.getLatitude(), location.getLongitude()); 156 //mMap.animateCamera(CameraUpdateFactory.newLatLng(curr)); 157 } 158 159 @Override 160 public void onProviderEnabled(String provider) { 161 } 162 163 @Override 164 public void onProviderDisabled(String provider) { 165 } 166 167 @Override 168 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 169 170 171 } 172 173
プログラミングの基礎をあまり知らないため、文構成がおかしいなどはあると思っています。
AndroidManifest
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.myapplication"> 4 5 <uses-permission android:name="android.permission.ACCESS_GPS" /> 6 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 7 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 8 9 10 <application 11 android:allowBackup="true" 12 android:icon="@mipmap/ic_launcher" 13 android:label="@string/app_name" 14 android:roundIcon="@mipmap/ic_launcher_round" 15 android:supportsRtl="true" 16 android:theme="@style/AppTheme"> 17 18 <!-- 19 The API key for Google Maps-based APIs is defined as a string resource. 20 (See the file "res/values/google_maps_api.xml"). 21 Note that the API key is linked to the encryption key used to sign the APK. 22 You need a different API key for each encryption key, including the release key that is used to 23 sign the APK for publishing. 24 You can define the keys for the debug and release targets in src/debug/ and src/release/. 25 --> 26 <meta-data 27 android:name="com.google.android.geo.API_KEY" 28 android:value="@string/google_maps_key" /> 29 30 31 <activity 32 android:name=".MainActivity" 33 android:label="@string/app_name" 34 android:theme="@style/AppTheme.NoActionBar" 35 > 36 <intent-filter> 37 <action android:name="android.intent.action.MAIN" /> 38 39 <category android:name="android.intent.category.LAUNCHER" /> 40 </intent-filter> 41 </activity> 42 <activity 43 android:name=".MapsActivity" 44 android:label="@string/title_activity_maps" 45 android:theme="@style/Widget.AppCompat.ActionBar"> 46 47 </activity> 48 </application> 49 50</manifest>
このコードが足りないなど、教えていただきたいです。
よろしくお願いします。
あなたの回答
tips
プレビュー