你好,我想使用getlocation创建一个在线附件一切正常,直到我关闭应用程序并忘记打开gps服务
每次我打开获取地理位置代码的活动总是失败,我要问的是,如何使用google maps api获取当前位置,以便每次我打开应用程序都能获取位置地理代码
请帮助我努力解决这个问题,我需要这个应用程序为我的最终项目
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.sistemabsensi.online.Absensi.onConnected(Absensi.java:415)
at com.google.android.gms.common.internal.GmsClientEventManager.onConnectionSuccess(Unknown Source:33)
at com.google.android.gms.common.api.internal.zaaw.zab(Unknown Source:292)
at com.google.android.gms.common.api.internal.zaak.zaaq(Unknown Source:125)
at com.google.android.gms.common.api.internal.zaak.onConnected(Unknown Source:105)
at com.google.android.gms.common.api.internal.zabe.onConnected(Unknown Source:101)
at com.google.android.gms.common.api.internal.zaq.onConnected(Unknown Source:6)
at com.google.android.gms.common.internal.zaf.onConnected(Unknown Source:2)
at com.google.android.gms.common.internal.BaseGmsClient$zzf.zzm(Unknown Source:24)
at com.google.android.gms.common.internal.BaseGmsClient$zza.zza(Unknown Source:12)
at com.google.android.gms.common.internal.BaseGmsClient$zzc.zzo(Unknown Source:11)
at com.google.android.gms.common.internal.BaseGmsClient$zzb.handleMessage(Unknown Source:49)
at android.os.Handler.dispatchMessage(Handler.java:107)
at com.google.android.gms.internal.common.zze.dispatchMessage(Unknown Source:8)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
I/Process: Sending signal. PID: 27425 SIG: 9
这是我的密码:
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
}
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
@SuppressLint("MissingPermission") Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latit = location.getLatitude();
double longit = location.getLongitude();
LatLng kantor = new LatLng(-8.4603894,118.7560158);
LatLng User = new LatLng(latit,longit);
distance=SphericalUtil.computeDistanceBetween(User,kantor);
TextView jarak = findViewById(R.id.jarak);
jarak.setText("Jarak Dari Kantor :" + Math.round(distance) + " Meter");
Log.d(TAG,"Latitude" + String.valueOf(latit) +"Longitude" + String.valueOf(longit));
在位置上的改变很好,但这不是我预期的
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latLng.latitude, latLng.longitude)).zoom(16).build();
latitude = location.getLatitude();
longitude = location.getLongitude();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
@SuppressLint("MissingPermission") Location location1 = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latit = location.getLatitude();
double longit = location.getLongitude();
float[] results = new float[1];
Location.distanceBetween(latit,longit,-8.4604926,118.7560354,results);
float distance = results[0];
int meter = (int) (distance/10000);
TextView jarak = findViewById(R.id.jarak);
1条答案
按热度按时间w7t8yxp51#
android 10引入了新的权限,称为“背景位置”:https://developer.android.com/training/location/permissions#background
对于android 10或以上设备,要在应用程序处于后台时获取位置,请执行以下操作:
您需要在清单中声明后台位置权限。
用户必须在运行时授予此权限。
(更新)必须在设备上启用位置服务。
(更新)样本代码:
在中声明罚款和背景权限
AndroidManifest.xml
(在manifest
标签):这是您向您的用户请求这些权限的方式
Activity
类(android 6之前没有权限):这是检查android 10+设备活动结果的方式(2级权限):
对于android 6-9设备,您只需检查
Manifest.permission.ACCESS_FINE_LOCATION