首先很抱歉问这个愚蠢的问题。由于我是Android的新手所以我正在尝试创建一个应用程序,其中首先会打开对话框,要求允许打开像Google地图或ola这样的GPS。然后,我想获得用户的经纬度,但不幸的是我尝试了搜索这么多的链接,但我没有得到正确的解决方案。所以任何人都可以帮助我。
我的代码是:
public class GpsPlaceSearch extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener
{
Activity context;
GoogleApiClient googleApiClient, getGoogleApiClient;
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
LocationRequest locationRequest1;
Location location;
private double currentLatitude;
private double currentLongitude;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
public GpsPlaceSearch(){
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_place_search);
getGoogleApiClient = new GoogleApiClient.Builder(this)
class will handle connection stuff
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
locationRequest1=locationRequests();
}
public GpsPlaceSearch(Activity context){
this.context=context;
googleApiClient=getInstance();
if (googleApiClient!=null){
settingsrequest();
googleApiClient.connect();
}
}
public LocationRequest locationRequests(){
LocationRequest locationRequest=LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
return locationRequest;
}
public GoogleApiClient getInstance(){
GoogleApiClient mGoogleApiClient=new GoogleApiClient.Builder(context).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
return mGoogleApiClient;
}
public void settingsrequest(){
Log.e("settingsrequest","Comes");
locationRequest1=locationRequests();
LocationSettingsRequest.Builder builder=new LocationSettingsRequest.Builder().addLocationRequest(locationRequest1);
builder.setAlwaysShow(true);
final PendingResult<LocationSettingsResult> result= LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
Status status=result.getStatus();
LocationSettingsStates states=result.getLocationSettingsStates();
switch (status.getStatusCode()){
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try{
status.startResolutionForResult(context,REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onConnected(Bundle bundle) {
location=LocationServices.FusedLocationApi.getLastLocation(getGoogleApiClient);
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},108);
LocationServices.FusedLocationApi.requestLocationUpdates(getGoogleApiClient,locationRequest1,this);
}
else {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Toast.makeText(this, currentLatitude + " AND " + currentLongitude + "", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()){
try {
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
else {
Log.e("Error", "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
@Override
protected void onStart() {
super.onStart();
Activity mActivity=this;
GpsPlaceSearch gpsPlaceSearch=new GpsPlaceSearch(mActivity);
}
@Override
protected void onResume() {
super.onResume();
//Now lets connect to the API
getGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
if (getGoogleApiClient.isConnected()){
LocationServices.FusedLocationApi.removeLocationUpdates(getGoogleApiClient,this);
getGoogleApiClient.disconnect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
LocationSettingsStates states=LocationSettingsStates.fromIntent(data);
switch (requestCode){
case REQUEST_CHECK_SETTINGS:
switch (resultCode){
case Activity.RESULT_OK:
break;
case Activity.RESULT_CANCELED:
startActivity(new Intent(getApplicationContext(),Login_page.class));
break;
}
break;
}
}
@Override
public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "", Toast.LENGTH_LONG).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
}
else {
}
return;
}
}
}
Logcat:
致命的例外:主要 过程:com.bigfoot.trialapp,PID:17660 java.lang.IllegalArgumentException:GoogleApiClient参数是 需要。 在com.google.android.gms.common.internal.zzaa.zzb(未知来源) 在com.google.android.gms.location.LocationServices.zzj(未知 资源) 在 com.google.android.gms.location.internal.zzd.getLastLocation(未知 资源) 在 com.bigfoot.trialapp.GpsPlaceSearch.onConnected(GpsPlaceSearch.java:128) 在com.google.android.gms.common.internal.zzk.zzp(未知来源) 在com.google.android.gms.internal.zzrd.zzn(未知来源) 在com.google.android.gms.internal.zzrb.zzass(未知来源) 在com.google.android.gms.internal.zzrb.onConnected(未知来源) 在com.google.android.gms.internal.zzrf.onConnected(未知来源) 在com.google.android.gms.internal.zzqr.onConnected(未知来源) 在com.google.android.gms.common.internal.zzj $ 1.onConnected(未知 资源) 在com.google.android.gms.common.internal.zze $ zzj.zzavj(未知 资源) 在com.google.android.gms.common.internal.zze $ zza.zzc(未知来源) 在com.google.android.gms.common.internal.zze $ zza.zzv(未知来源) 在com.google.android.gms.common.internal.zze $ zze.zzavl(未知 资源) 在 com.google.android.gms.common.internal.zze $ zzd.handleMessage(未知 资源) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
答案 0 :(得分:0)
您应该从 onConnected
中删除以下行 location=LocationServices.FusedLocationApi.getLastLocation(getGoogleApiClient);
并且只应在 onConnected
中调用 requestLocationUpdates 方法@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onConnected(Bundle bundle) {
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},108);
LocationServices.FusedLocationApi.requestLocationUpdates(getGoogleApiClient,locationRequest1,this);
}
}else{
//custom dialog for "please enable location permission to access device location"
}