【问题标题】:Which Google API to activate when using FusedLocationAPI使用 FusedLocationAPI 时要激活哪个 Google API
【发布时间】:2016-02-27 17:01:50
【问题描述】:

我是安卓新手。我正在尝试为我的应用程序使用 google api 来获取设备的位置。我发现 fusedlocation api 对此有好处,但我有点分心,当我打开 console.developers.com 并尝试为我的应用程序获取 google API 密钥时,我只是不知道要激活哪个 api,fusedlocation api 不存在在列表中。

所以我也不知道在哪里以及如何将 API 密钥放入我的应用程序中,我没有找到任何好的教程,我想它应该是字符串... 很抱歉,如果有人已经要求这个,我没有找到这样的东西。 感谢您的想法。

【问题讨论】:

  • 如果您只想使用 Fused Location Provider,则不需要任何这些 API。这些 API 需要激活,因为它们使用大量数据,其中一些会限制您。但 GPS 不使用数据。因此,请继续在您的应用程序中使用它。

标签: android api fusedlocationproviderapi


【解决方案1】:

融合位置 API 不需要 API 密钥。

【讨论】:

    【解决方案2】:

    我打开 console.developers.com 并尝试为我的应用程序获取 google API 密钥,我只是不知道要激活哪个 api,fusedlocation api 不在列表中。

    融合位置 api 不需要任何类型的 api 密钥

    你可以试试这个代码

       import android.app.Activity;
    import android.location.Location;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
    import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
    import com.google.android.gms.location.LocationListener;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationServices;
    
    public class MainActivity extends Activity implements ConnectionCallbacks,
            OnConnectionFailedListener, LocationListener {
    
        // LogCat tag
        private static final String TAG = MainActivity.class.getSimpleName();
    
        private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
    
        private Location mLastLocation;
    
        // Google client to interact with Google API
        private GoogleApiClient mGoogleApiClient;
    
        // boolean flag to toggle periodic location updates
        private boolean mRequestingLocationUpdates = false;
    
        private LocationRequest mLocationRequest;
    
        // Location updates intervals in sec
        private static int UPDATE_INTERVAL = 10000; // 10 sec
        private static int FATEST_INTERVAL = 5000; // 5 sec
        private static int DISPLACEMENT = 10; // 10 meters
    
        // UI elements
        private TextView lblLocation;
        private Button btnShowLocation, btnStartLocationUpdates;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            lblLocation = (TextView) findViewById(R.id.lblLocation);
            btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
            btnStartLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates);
    
            // First we need to check availability of play services
            if (checkPlayServices()) {
    
                // Building the GoogleApi client
                buildGoogleApiClient();
    
                createLocationRequest();
            }
    
            // Show location button click listener
            btnShowLocation.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    displayLocation();
                }
            });
    
            // Toggling the periodic location updates
            btnStartLocationUpdates.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    togglePeriodicLocationUpdates();
                }
            });
    
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            if (mGoogleApiClient != null) {
                mGoogleApiClient.connect();
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            checkPlayServices();
    
            // Resuming the periodic location updates
            if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
                startLocationUpdates();
            }
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            if (mGoogleApiClient.isConnected()) {
                mGoogleApiClient.disconnect();
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            stopLocationUpdates();
        }
    
        /**
         * Method to display the location on UI
         * */
        private void displayLocation() {
    
            mLastLocation = LocationServices.FusedLocationApi
                    .getLastLocation(mGoogleApiClient);
    
            if (mLastLocation != null) {
                double latitude = mLastLocation.getLatitude();
                double longitude = mLastLocation.getLongitude();
    
                lblLocation.setText(latitude + ", " + longitude);
    
            } else {
    
                lblLocation
                        .setText("(Couldn't get the location. Make sure location is enabled on the device)");
            }
        }
    
        /**
         * Method to toggle periodic location updates
         * */
        private void togglePeriodicLocationUpdates() {
            if (!mRequestingLocationUpdates) {
                // Changing the button text
                btnStartLocationUpdates
                        .setText(getString(R.string.btn_stop_location_updates));
    
                mRequestingLocationUpdates = true;
    
                // Starting the location updates
                startLocationUpdates();
    
                Log.d(TAG, "Periodic location updates started!");
    
            } else {
                // Changing the button text
                btnStartLocationUpdates
                        .setText(getString(R.string.btn_start_location_updates));
    
                mRequestingLocationUpdates = false;
    
                // Stopping the location updates
                stopLocationUpdates();
    
                Log.d(TAG, "Periodic location updates stopped!");
            }
        }
    
        /**
         * Creating google api client object
         * */
        protected synchronized void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API).build();
        }
    
        /**
         * Creating location request object
         * */
        protected void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(UPDATE_INTERVAL);
            mLocationRequest.setFastestInterval(FATEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
        }
    
        /**
         * Method to verify google play services on the device
         * */
        private boolean checkPlayServices() {
            int resultCode = GooglePlayServicesUtil
                    .isGooglePlayServicesAvailable(this);
            if (resultCode != ConnectionResult.SUCCESS) {
                if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                    GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                            PLAY_SERVICES_RESOLUTION_REQUEST).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "This device is not supported.", Toast.LENGTH_LONG)
                            .show();
                    finish();
                }
                return false;
            }
            return true;
        }
    
        /**
         * Starting the location updates
         * */
        protected void startLocationUpdates() {
    
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
    
        }
    
        /**
         * Stopping location updates
         */
        protected void stopLocationUpdates() {
            LocationServices.FusedLocationApi.removeLocationUpdates(
                    mGoogleApiClient, this);
        }
    
        /**
         * Google api callback methods
         */
        @Override
        public void onConnectionFailed(ConnectionResult result) {
            Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
                    + result.getErrorCode());
        }
    
        @Override
        public void onConnected(Bundle arg0) {
    
            // Once connected with google api, get the location
            displayLocation();
    
            if (mRequestingLocationUpdates) {
                startLocationUpdates();
            }
        }
    
        @Override
        public void onConnectionSuspended(int arg0) {
            mGoogleApiClient.connect();
        }
    
        @Override
        public void onLocationChanged(Location location) {
            // Assign the new location
            mLastLocation = location;
    
            Toast.makeText(getApplicationContext(), "Location changed!",
                    Toast.LENGTH_SHORT).show();
    
            // Displaying the new location on UI
            displayLocation();
        }
    
    }
    

    【讨论】:

    • 谢谢你们。我不会复制你的代码,因为我想学习它,你的代码肯定会帮助我。
    猜你喜欢
    • 2017-01-31
    • 1970-01-01
    • 2014-12-11
    • 2014-05-24
    • 2017-09-09
    • 2018-09-03
    • 1970-01-01
    • 2018-01-22
    • 2022-09-30
    相关资源
    最近更新 更多