【问题标题】:How to delete blue dot in android google map?如何删除安卓谷歌地图中的蓝点?
【发布时间】:2018-08-24 15:07:48
【问题描述】:

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

【问题讨论】:

  • 无法移除蓝点并仍然使用 googleMap 对象获取位置信息。如果你想同时实现,那么就做 mMap.setMyLocationEnabled(false);并使用您自己的 LocationManager 实现获取位置
  • 您能告诉我使用 LocationManager 的正确示例吗?
  • 网上有很多例子,如果想深入了解,请到this
  • 你能给我github链接吗?
  • 您好,我也有类似的问题,我尝试了 mMap.setMyLocationEnabled(false);和 mmap.getUiSettings().setMyLocationButtonEnabled(false);但是蓝点仍然存在,甚至找不到删除它的方法,请帮助

标签: android google-maps


【解决方案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);

}}

【讨论】:

  • 很好的答案!效果很好!我添加了以下代码。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission. ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 2102); } }
猜你喜欢
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多