使用Google Play服务获得的位置是错误的

时间:2014-09-08 16:10:29

标签: android geolocation google-play-services

我按照developer.android.com文档创建了以下代码,以使用Play服务获取位置。一切都很顺利,但是当我测试使用 - http://maps.googleapis.com/maps/api/geocode/json?latlng=latitudevalue,longitudevalue获得的纬度和经度时,我找错了地方。

由于代码没有发出任何错误,我不知道如何找到Play服务出错的原因!!

这是我的代码

public class MainActivity extends Activity implements 
                        GooglePlayServicesClient.ConnectionCallbacks,
                        GooglePlayServicesClient.OnConnectionFailedListener,
                        LocationListener{

    //Location Objects
    LocationClient mLocationClient;
    LocationRequest mLocationRequest;
    Location mCurrentLocation;

    TextView txtLong, txtLat;

    static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Getting reference to the TextView
        txtLong = (TextView) findViewById(R.id.txtLong);
        txtLat = (TextView) findViewById(R.id.txtLat);

        //Creating LocationClient
        mLocationClient = new LocationClient(this, this, this);

        //Creating and setting LocationRequest for Location Update
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        //Set the update interval to 5 seconds
        mLocationRequest.setInterval(1000*5);

        //Set the fastest update interval to 1 second
        mLocationRequest.setFastestInterval(1000*1);
    }

    /*@Override
    protected void onStart() {
        super.onStart();

    }*/

    @Override
    protected void onResume() {
        super.onResume();
        if(checkPlayServices()){
            //Connecting the client
            mLocationClient.connect();

        }
    }

    private boolean checkPlayServices() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if(status != ConnectionResult.SUCCESS) {
            if(GooglePlayServicesUtil.isUserRecoverableError(status)) {

                showErrorDialog(status);

            }else {

                Toast.makeText(this, "This Device is not Supported", Toast.LENGTH_LONG).show();
                finish();
            }
            return false;
        }
        return true;

    }

    private void showErrorDialog(int status) {
        GooglePlayServicesUtil.getErrorDialog(status, this, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
        case REQUEST_CODE_RECOVER_PLAY_SERVICES:
            if(resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Google Play Services must be installed", Toast.LENGTH_LONG).show();
                finish();

            }
            return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //Disconnecting the client thereby invalidating it
        mLocationClient.disconnect();
    }

    //Implemented by PlayserviceClient.ConnecitonCallback interface
    @Override
    public void onConnected(Bundle connectionHint) {

        /*if(mLocationClient != null)
            mLocationClient.requestLocationUpdates(mLocationRequest, this);
        Toast.makeText(this ,"Connected", Toast.LENGTH_LONG).show();*/

        if(mLocationClient !=null ){

            mLocationClient.requestLocationUpdates(mLocationRequest, this);
            Toast.makeText(this ,"Connected", Toast.LENGTH_LONG).show();

            //Get the location
            mCurrentLocation = mLocationClient.getLastLocation();
            try{

                //seting TextViews
                txtLat.setText(mCurrentLocation.getLatitude()+"");
                txtLong.setText(mCurrentLocation.getLongitude()+"");
            }catch(NullPointerException e){

                Toast.makeText(this,"Failed to Connect" , Toast.LENGTH_LONG).show();
                Intent mIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(mIntent);

            }
        }

    }

    @Override
    public void onDisconnected() {
        Toast.makeText(this,"Disconnected" , Toast.LENGTH_LONG).show();

    }

    //Implemented by PlayserviceClient.onConnectionFailedListener interface
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Toast.makeText(this, "Connection Failer", Toast.LENGTH_LONG).show();
    }

    //Implemented by onLocationChanged interface
    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(this,"Location Changed" , Toast.LENGTH_LONG).show();
        mCurrentLocation = mLocationClient.getLastLocation();
        txtLat.setText(mCurrentLocation.getLatitude()+"");
        txtLong.setText(mCurrentLocation.getLongitude()+"");
    }
}

1 个答案:

答案 0 :(得分:0)

文本视图首先显示经度,然后显示纬度。我在浏览器中使用的测试网址,先接受纬度,然后接受经度。不匹配把我从地球的一部分带到了另一部分。 无论如何,它解决了。