我尝试使用GoogleClientAPI获取用户位置的纬度和经度。我没有看到任何错误,但我无法记录位置详细信息。
我刚刚在几周后开始使用android。
我在下面附上了我的代码:
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
@InjectView(R.id.idTemparatureValue) TextView mTemparatureValue;
@InjectView(R.id.idHumidityValue) TextView mHumidityValue;
@InjectView(R.id.idRainValue) TextView mRainValue;
@InjectView(R.id.idSummaryValue) TextView mSummaryValue;
@InjectView(R.id.idTimeValue) TextView mTimeValue;
private final String TAG=MainActivity.class.getSimpleName();
GoogleApiClient mGoogleApiClient;
private String apiKey="802**********a1fdf5";
private double latitude;/* =13.0827;*/
private double longitude ;/*=80.2707;*/
Forecast mForecast = new Forecast();
private String url;/*="https://api.forecast.io/forecast/"+apiKey+"/"+latitude+","+longitude;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient=new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mGoogleApiClient.connect();
ButterKnife.inject(this);
.......
protected void onResume(){
super.onResume();
mGoogleApiClient.connect();
}
protected void onPause(){
super.onPause();
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnected(Bundle bundle) {
Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location!=null){
handleLocation(location);
}
}
private void handleLocation(Location location) {
Log.i(TAG,location.toString());
Log.i(TAG,"Latitude : "+location.getLatitude());
Log.i(TAG,"Longitude : "+location.getLongitude());
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}