代码之家  ›  专栏  ›  技术社区  ›  cabanapi

在中国的Android可穿戴设备中获取GPS位置

  •  1
  • cabanapi  · 技术社区  · 7 年前

    我的目标是获取设备的GPS位置并跟踪其运动,但我似乎无法找到如何使其工作。

    目前,我正在为API 21开发,我正在使用以下谷歌Play服务库,因为它是中国可穿戴设备使用的库: com.google.android.gms:play-services-wearable:7.8.87

    我使用的代码如下:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_on_route);
    
        locationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(UPDATE_INTERVAL_MS)
                .setFastestInterval(FASTEST_INTERVAL_MS);
    
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addApi(Wearable.API)  // used for data layer API
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    
        startTravelTime = System.currentTimeMillis();
    
        if (!hasGps()) {
            Log.d(TAG, "This hardware doesn't have GPS.");
        }
    }
    
    private boolean hasGps() {
        return getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
        Log.i("Connected!!!", "WERE CONNECTED");
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
        mGoogleApiClient.disconnect();
        System.out.println("onstop");
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        if(!mGoogleApiClient.isConnected()) {
            mGoogleApiClient.connect();
        }
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, OnRouteActivity.this);
        }
        mGoogleApiClient.disconnect();
    }
    
    @Override
    public void onConnected(Bundle bundle) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this).setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        if (status.getStatus().isSuccess()) {
                            if (Log.isLoggable(TAG, Log.DEBUG)) {
                                Log.d(TAG, "Successfully requested location updates");
                            }
                        } else {
                            Log.e(TAG,
                                    "Failed in requesting location updates, "
                                            + "status code: "
                                            + status.getStatusCode()
                                            + ", message: "
                                            + status.getStatusMessage());
                        }
                    }
                });
    }
    
    @Override
    public void onLocationChanged(Location location){
    
        // Display the latitude and longitude in the UI
        mTextView = (TextView) findViewById(R.id.location);
        mTextView.setText("Latitude:  " + String.valueOf( location.getLatitude()) + "\nLongitude:  " + String.valueOf( location.getLongitude()));
        Toast.makeText(getBaseContext(), "Latitude:  " + String.valueOf( location.getLatitude()) + "\nLongitude:  " + String.valueOf( location.getLongitude()), Toast.LENGTH_LONG).show();
    }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   TofferJ    7 年前

    Fused Location Provider API 并确保在构建中包含以下行。gradle文件:

    dependencies {
        ...
        compile 'com.google.android.gms:play-services-location:10.2.0'
    }
    

    dependencies {
        ...
        compile 'com.google.android.gms:play-services-wearable:10.2.0'
    }
    

    Creating Android Wear Apps for China