2016-06-13 7 views
1

エミュレータで何が問題なのか分かりません。私は、GPSとGoogle Playのサービスをサポートしますが、何も現在のlocaionを取るために動作しない私のエミュレータでは、このコマンドでLocationServices.FusedLocationApi.getLastLocation(googleApiClient)はエミュレータで常にnullです。

LocationServices.FusedLocationApi.getLastLocation(googleApiClient) 

をlocatioan取ることはできません。 location = null。助けが必要。ここに私のコード:

private void configureGoogleApiClient() { 
     locationRequest = LocationRequest.create(); 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     locationRequest.setInterval(Constant.INTERVAL); 
     locationRequest.setFastestInterval(Constant.FASTINTERVAL); 

     googleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
     if (googleApiClient != null) { 
      googleApiClient.connect(); 
     } 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     if (googleApiClient != null) { 
      googleApiClient.connect(); 
     } 
    } 

    protected void onStop() { 
     // Disconnecting the client invalidates it. 
     if (googleApiClient != null) { 
      googleApiClient.disconnect(); 
     } 
     super.onStop(); 
    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (googleApiClient != null) { 
      googleApiClient.disconnect(); 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      if (googleApiClient != null) 
       googleApiClient.connect(); 
     } 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     if (googleApiClient.isConnected()) { 
      Log.e(MainActivity.class.getSimpleName(), "Is connected"); 
     } else 
      Log.e(MainActivity.class.getSimpleName(), "Is not connected"); 

     if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      Log.e(MainActivity.class.getSimpleName(), "Is connected on GPS"); 
     } else { 
      Log.e(MainActivity.class.getSimpleName(), "Is not connected on GPS"); 
     } 

     Utility.checkPermission(this); 
     location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 

     if (location == null) { 
      Utility.checkPermission(this); 
      LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
     } 

     if (location != null) { 
      latText.setText("Lat: " + String.valueOf(location.getLatitude())); 
      longText.setText("Long: " + String.valueOf(location.getLongitude())); 
     } else { 
      latText.setText(getString(R.string.error_find_location)); 
      longText.setText(getString(R.string.error_find_location)); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     Log.e(MainActivity.class.getSimpleName(), connectionResult.toString()); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     currentLocation = location; 
     if (googleApiClient != null) 
      if (googleApiClient.isConnected() || googleApiClient.isConnecting()) { 
       googleApiClient.disconnect(); 
       googleApiClient.connect(); 
      } else if (!googleApiClient.isConnected()) { 
       googleApiClient.connect(); 
      } 
     String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()); 
     Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
     configureView(); 
    } 
+0

どのシミュレータを使用していますか? –

+0

私はAVDでこのエミュレータのプロパティを使用しています:Nexus 5、api 22、Android 5.1 @JayShah –

答えて

1

私の推測では、最後の位置は最後の位置だけを示していると思います。実際に場所を把握しようとはしません。あなたの携帯電話で別のアプリがおそらく場所を要求しました。したがって、最後の場所があります。

場所がない場合は、このコードを追加して場所をリクエストしました。

Location userLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
if (userLocation == null) { 
    LocationRequest locationRequest = new LocationRequest(); 
    locationRequest.setNumUpdates(1); 
    locationRequest.setInterval(0); 
    locationRequest.setPriority(PRIORITY_HIGH_ACCURACY); 
    LocationServices.FusedLocationApi.requestLocationUpdates(
      googleApiClient, locationRequest, 
      new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 
        LocationServices.FusedLocationApi.removeLocationUpdates(
          googleApiClient, 
          this); 
       } 
      }); 
関連する問題