getLastLocation() 返回俄罗斯中部的随机位置
Posted
技术标签:
【中文标题】getLastLocation() 返回俄罗斯中部的随机位置【英文标题】:getLastLocation() returns a random location in the middle of russia 【发布时间】:2021-01-17 05:20:09 【问题描述】:我试图通过 SMS 向用户发送最后知道的位置,但 lastKnownLocation() 不断返回 null 或随机位置,位于俄罗斯海岸附近的偏远地区,感谢任何帮助! 我早些时候试图通过 LocationManager 获取位置,但后来开始每次都重新调整 null。 我正在遵循文档中给出的步骤 https://developer.android.com/training/location/retrieve-current
package com.r30.wec_task;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class SosActivity extends AppCompatActivity
private static final int REQUEST_LOCATION = 1;
private static final int REQUEST_SMS = 1;
private FusedLocationProviderClient fusedLocationClient;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("users").child(user.getUid());
// LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sos2);
ActivityCompat.requestPermissions( this,
new String[] Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_LOCATION);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
String emergencyContactNo;
@Override
protected void onStart()
super.onStart();
ImageButton sosBtn = findViewById(R.id.sosButton);
//Get Emergency Contact from DataBase
userRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
emergencyContactNo = snapshot.child("eContact").getValue(String.class).toString();
Toast.makeText(SosActivity.this,emergencyContactNo,Toast.LENGTH_SHORT);
@Override
public void onCancelled(@NonNull DatabaseError error)
Toast.makeText(SosActivity.this,"Error getting your emergency contact",Toast.LENGTH_SHORT);
);
//Button Click sends SOS signal + GPS Co-ordinates
sosBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//get Location of user
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
OnGPS();
else
sendLocation();
);
private void OnGPS()
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
).setNegativeButton("No", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
dialog.cancel();
);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
String ans;
private void sendLocation()
if (ActivityCompat.checkSelfPermission(
SosActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
SosActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_LOCATION);
else
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>()
@Override
public void onSuccess(Location location)
// Got last known location. In some rare situations this can be null.
if (location != null)
// Logic to handle location object
ans=location.getLongitude()+","+location.getLongitude();
Intent intent=new Intent(SosActivity.this,SosActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
//Get the SmsManager instance and call the sendTextMessage method to send message
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(emergencyContactNo, null, "SOS my location is http://maps.google.com/maps?q="+ans, pi,null);
Toast.makeText(getApplicationContext(), "Message Sent successfully!"+emergencyContactNo,
Toast.LENGTH_LONG).show();
);
【问题讨论】:
【参考方案1】:您使用了两次经度:
ans=location.getLongitude()+","+location.getLongitude();
应该是:
ans=location.getLatitude()+","+location.getLongitude();
【讨论】:
非常感谢!!我已经让 7 个人检查了我的代码,不敢相信没有人发现这个。以上是关于getLastLocation() 返回俄罗斯中部的随机位置的主要内容,如果未能解决你的问题,请参考以下文章
来自 LocationClient 的 getLastLocation() 始终返回 null [重复]
Android studio 不检查 mFusedLocationProviderClient.getLastLocation().addOnSuccessListener 并返回当前位置 null
从 Settings Api 对话框打开位置后,getLastLocation() 返回 null
Android LocationClient.getLastLocation() 返回带有新时间戳的旧且不准确的位置
FusedLocationApi.getLastLocation 总是返回 null(android 6.0,api 23)