如何在Android Google Map中删除蓝点?

时间:2018-08-24 15:07:49

标签: android google-maps

我想在Android Google地图中删除蓝点。 我尝试了mMap.setMyLocationEnabled(false); 但我无法获得位置信息。 我试图找到正确的方法,但没有得到正确的答案。 我希望正确答案和示例项目。 谢谢 最好的问候

1 个答案:

答案 0 :(得分:1)

使用此位置跟踪器

public class PlayServicesLocationTracker implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

private Context mContext;

private OnLocationFetchListener mListener;

public PlayServicesLocationTracker(Activity context) {
    this.mContext = context;
    if (checkPlayServices()) {
        buildGoogleApiClient();
        createLocationRequest();
        checkLocationEnabled(context);
        onStart();
    }
}

public PlayServicesLocationTracker(Context context) {
    this.mContext = context;
    if (checkPlayServices()) {
        buildGoogleApiClient();
        createLocationRequest();
        onStart();
    }
}

/**
 * Method used to set Location listener
 *
 * @param listener
 */
public void setLocationListener(OnLocationFetchListener listener) {
    mListener = listener;
}

public void onStart() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}

public void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
        stopLocationUpdates();
    }
}

/**
 * Method to display the location on UI
 */
private void displayLocation() {

    Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (lastLocation != null) {
        if (mListener != null)
            mListener.onLocationChanged(lastLocation);
    }
}

/**
 * Creating google api client object
 */
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(mContext)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();
}

/**
 * Creating location request object
 */
protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(10);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setSpeedRequired(true);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
}

/**
 * Method to verify google play services on the device
 */
private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (resultCode != ConnectionResult.SUCCESS) {
        Toast.makeText(mContext, "This device is not supported.", Toast.LENGTH_LONG).show();
        return false;
    }
    return true;
}

/**
 * Starting the location updates
 */
protected void startLocationUpdates() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
    }

}

/**
 * Stopping location updates
 */
protected void stopLocationUpdates() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected())
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient, this);
}

@Override
public void onConnectionFailed(ConnectionResult result) {
}

@Override
public void onConnected(Bundle arg0) {
    displayLocation();
    startLocationUpdates();
}

@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {
    //Toast.makeText(mContext, "Got location", Toast.LENGTH_SHORT).show();
    displayLocation();
}

public void checkLocationEnabled(final Activity activity) {

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                activity,
                                MenuActivity.LOCATION_DIALOG_REQUEST_CODE);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}

public interface OnLocationFetchListener {

    void onLocationChanged(Location location);

}}