android - 如何删除安卓谷歌地图中的蓝点?
问题描述
我想删除 android 谷歌地图中的蓝点。我试过 mMap.setMyLocationEnabled(false);
了,但我无法获取位置信息。我试图找到正确的方法,但没有得到正确的答案。我希望正确的答案和示例项目。谢谢最好的问候
解决方案
使用此位置跟踪器
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);
}}
推荐阅读
- php - WooCommerce 未在购物车页面上显示不含税的实际价格
- android - 检查用户是否在 Android 中更改了生物识别/指纹
- javascript - 使用 JavaScript 在网站上执行任务
- amazon-web-services - 在远程服务器上使用 aws 凭据而不将它们存储在远程服务器上?
- javascript - javascript 从表中获取/存储数据
- boost-multi-array - 如何获取 boost multi_array 的视图并将其分配回相同的 boost multi_array?
- r - 回归中的观察次数
- javascript - 如何从 Python 调用 Javascript 函数
- mysql - MySQL内连接获取连接表的最后一项
- entity-framework-migrations - 为什么 Migration.BuildTargetModel 有这么多重复代码?