2016-10-03 18 views
0

私は現在のロケーションアップデートを取得しようとしており、Googleが提案したように融合ロケーションプロバイダを使用しましたが、私はアップデートを得ることができません。私はWeb上で完全に検索しましたが、解決策は見つかりませんでした。融合したプロバイダによって返される場所は、私がどこにいるかにも近くないので、他の国を示しています。誰でもここで私を助けることができますか?融合ロケーションプロバイダは常に同じ場所を返します

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

をgetLatitudeない、getLongitudeでなければなりません。 – mallaudin

+0

間隔と最速間隔を短くして確認してください。 –

+0

試しました。運がない。常に33.xxxx、33.xxxxを返す – mallaudin

答えて

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

2番目のパラメータは、すでに確認さ

+0

グレートキャッチ男... –

+0

ハ、私はそれを最初に得ました。 – mallaudin

関連する問題