无法使用GoogleApiClient获取当前纬度和经度

时间:2015-08-20 07:33:35

标签: java android

我使用了以下代码,我从模拟器Gps传递纬度和经度。

  $(window).scrollTop();

但我在屏幕上没有得到任何东西。 但是在Log中,我正在寻找以下内容。

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;


public class MainActivity extends Activity implements
    GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

GoogleApiClient mGoogleApiClient;
Location mLastLocation;
TextView tvLatlong;

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

    tvLatlong = (TextView) findViewById(R.id.tvLatlong);

    buildGoogleApiClient();

    if(mGoogleApiClient!= null){
        mGoogleApiClient.connect();
    }
    else
        Toast.makeText(this, "Not connected...", Toast.LENGTH_SHORT).show();


}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    Toast.makeText(this, "Failed to connect...", Toast.LENGTH_SHORT).show();

}

@Override
public void onConnected(Bundle bundle) {

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);

    if (mLastLocation != null) {
        tvLatlong.setText("Latitude: "+ String.valueOf(mLastLocation.getLatitude())+"Longitude: "+
                String.valueOf(mLastLocation.getLongitude()));
    }

}

@Override
public void onConnectionSuspended(int arg0) {
    Toast.makeText(this, "Connection suspended...", Toast.LENGTH_SHORT).show();

}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
 }
}

Ide Used:Android Studio

使用的仿真器:Genymotion(Galaxy note 2 4.3 api 18)

使用的依赖关系:

com.android.support:appcompat-v7:23.0.0

com.google.android.gms:播放服务:7.8.0'

0 个答案:

没有答案