【问题标题】:Location obtained using Google Play Service is wrong使用 Google Play 服务获取的位置错误
【发布时间】:2014-09-08 16:10:29
【问题描述】:

我遵循 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()+"");
    }
}

【问题讨论】:

  • 关闭wifi再试一次。
  • @323go 试过了。仍然给出了相同的位置 - 斯瓦尔巴群岛和扬马延,相信我,我离那里还很远。
  • 我有一种预感,可能是 wifi 定位问题。抱歉,无法提供其他帮助,因为这可能是本地问题。
  • @323go 我将尝试直接将纬度和经度转换为手机本身的地址,而不是通过浏览器重做。可能会带来一些见解。
  • 似乎是一个好方法。祝你好运!

标签: android geolocation google-play-services


【解决方案1】:

文本视图首先显示经度,然后显示纬度。我在浏览器中使用的测试网址,首先接受纬度,然后接受经度。这种不匹配把我从地球的一个地方带到了另一个地方。 总之,解决了。

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 2018-10-09
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 2019-03-20
    相关资源
    最近更新 更多