2016-10-03 3 views
0

Ich habe versucht, meine aktuellen Standortaktualisierungen zu erhalten und den Fused location provider wie von Google vorgeschlagen zu verwenden, aber ich kann kein Update erhalten. Ich habe gründlich im Internet gesucht, konnte aber keine Lösung finden. Der vom fusionierten Anbieter zurückgegebene Standort ist nicht einmal in der Nähe, wo ich bin, es zeigt ein anderes Land. Kann mir hier jemand helfen?Fused location provider gibt denselben Standort zurück

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener , 
     LocationListener, GoogleApiClient.ConnectionCallbacks{ 

    private LocationRequest mLocationRequest; 
    private GoogleApiClient mGoogleApiClient; 
    private TextView mLocationTextView; 

    private static final int LOC_PERMISSION_CODE = 100; 
    private Location mCurrentLocation; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     createLocationRequest(); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
     mGoogleApiClient.connect(); 


     mLocationTextView = (TextView) findViewById(R.id.location); 
    } 

    private boolean isLocationProviderAvailable(){ 
     LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
     return lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } 

    private void requestLocation() { 
     if (!hasLocationPermission()) { 
      ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, LOC_PERMISSION_CODE); 
     }else { 

      if(!isLocationProviderAvailable()){ 
       AlertDialog dialog = new AlertDialog.Builder(this) 
         .setTitle("No location service") 
         .setMessage("Location adapters are turned off. Please turn on and try again.") 
         .setIcon(R.drawable.location_icon) 
         .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
           finish(); 
          } 
         }) 
         .create(); 
       dialog.show(); 
      }else { 
       //noinspection MissingPermission 
       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
      } 
     } 
    } 
    private boolean hasLocationPermission(){ 
     return ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; 
    } 

    private void createLocationRequest(){ 
     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setInterval(1000 * 10); 
     mLocationRequest.setFastestInterval(1000 * 5); 
     mLocationRequest.setNumUpdates(2); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 

     if(requestCode == LOC_PERMISSION_CODE){ 
      if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
       requestLocation(); 
      }else { 
       Toast.makeText(this, "Permission Required.", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 


    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     log("onConnectionFailed -> %s", connectionResult.getErrorMessage()); 
    } 

    private void log(String format, Object... args){ 
     Log.d("MainActivity", String.format(format, args)); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     mCurrentLocation = location; 
     log("onLocationChanged()"); 
     updateUI(); 
    } 

    private void updateUI(){ 
     log("updateUI()"); 
     if(null != mCurrentLocation) { 
      mLocationTextView.setText(String.format("Lat: %s, Lon: %s", mCurrentLocation.getLatitude(), mCurrentLocation.getLatitude())); 
     } 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     requestLocation(); 
     log("onConnected()"); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     log("onConnectionSuspended()"); 
    } 
} 
+0

bereits geprüft. – mallaudin

+0

Versuchen Sie, das Intervall und das schnellste Intervall zu reduzieren, und überprüfen Sie. –

+0

Versucht. Kein Glück. Es gibt immer 33.xxxx, 33.xxxx – mallaudin

Antwort

1
"Lat: %s, Lon: %s", mCurrentLocation.getLatitude(), mCurrentLocation.getLatitude()) 

Der zweite Parameter sollte getLongitude, nicht getLatitude

+0

Grate fangen Mann ... –

+0

Hah, ich habe es zuerst. – mallaudin

Verwandte Themen