【发布时间】:2016-08-05 19:52:39
【问题描述】:
到目前为止。它应该在两个单独的文本视图中将当前位置显示为纬度和经度。
我已经在模拟器和真正的 android 手机上尝试过,但两者都不起作用。没有任何反应,文本视图不显示任何坐标。也没有错误。
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final int LOCATION_REQUEST_INTERVAL = 60000;
private static final int LOCATION_REQUEST_FASTEST = 15000;
private static final int MY_PERMISSION_REQUEST_READ_COARSE_LOCATION = 102;
private TextView latitudeTextView;
private TextView longitudeTextView;
private ImageButton imageButton;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
private double mLatitude;
private double mLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTextView = (TextView) findViewById(R.id.latitudeTextView);
longitudeTextView = (TextView) findViewById(R.id.longitudeTextView);
imageButton = (ImageButton) findViewById(R.id.imageButton);
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
locationRequest = new LocationRequest();
locationRequest.setInterval(LOCATION_REQUEST_INTERVAL);
locationRequest.setFastestInterval(LOCATION_REQUEST_FASTEST);
locationRequest.setPriority(locationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
requestLocationUpdates();
}
private void requestLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_REQUEST_READ_COARSE_LOCATION);
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
latitudeTextView.setText(String.valueOf(mLatitude));
longitudeTextView.setText(String.valueOf(mLongitude));
}
@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
googleApiClient.disconnect();
}
@Override
protected void onResume() {
super.onResume();
if(googleApiClient.isConnected())
requestLocationUpdates();;
}
@Override
protected void onPause() {
super.onPause();
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
}
}
为什么它不起作用?我遵循了一些指南,其中之一是 android 开发者指南。
您有什么想法或建议吗?
感谢您的任何意见。
【问题讨论】:
-
您需要使用
Log.d来识别哪一行代码不能正常工作。一旦你找到一个意想不到或没有价值的点,然后发布问题。所以在每一行后面加上一个Log.d语句。如果您不弄清楚到底是什么不起作用,就要求浪费他们的时间是没有意义的。
标签: java android geolocation location google-play-services