Android:Google地图位置,电量不足

前端之家收集整理的这篇文章主要介绍了Android:Google地图位置,电量不足前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程式正在使用Google Play服务的Google地图

speficying:

mMap.setMyLocationEnabled(真);

每当我在我的应用程序中显示地图时,我都意识到:

>位置在地图上用蓝点表示
>位置图标显示在顶部栏中
>如果我进入手机的设置/位置,我的应用程序被报告为“高电量使用”

不过,我可以看到有地图使用的应用程序仍然显示位置蓝点,但是位置图标不会显示在顶栏中,而且电池的使用量很低.

我的应用程序目前授予两个权限:

> android.permission.ACCESS_COARSE_LOCATION
> android.permission.ACCESS_FINE_LOCATION

我的问题是:

如何在低电量使用情况下显示位置蓝点?

是否可以通过代码指定精度/电池使用量?

UPDATE

其实我意识到这样做是使用GoogleApiClient的FusedLocationApi

mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

我在我的活动中配置了GoogleApiClient,调用

> GoogleApiClient.connect()在Activity的开始
> GoogleApiClient.disconnect()在活动停止

在onConnected回调我设置的位置更新的标准:最快的间隔1分钟低功率优先级:

private static final LocationRequest REQUEST = LocationRequest.create()
        .setFastestInterval(60000)   // in milliseconds
        .setInterval(180000)         // in milliseconds
        .setPriority(LocationRequest.PRIORITY_LOW_POWER);

    @Override
    public void onConnected(Bundle bundle) {
        LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient,REQUEST,this);  // LocationListener
    }

我测试过,GoogleApiClient在开始时正确连接,但是由于某些原因,每当我使用嵌入式MapView访问片段时,我仍然可以在“设置/位置”屏幕上获得我的应用程序的高电量使用!

MapView似乎忽略了这些低功耗标准!

解决方法

最终发现解决方案!
谢谢特里斯坦的回答!

默认情况下,Google地图使用位置提供商,而不是融合位置提供商.为了使用融合位置提供程序(允许您控制位置精度和功耗),您需要使用GoogleMap.setLocationSource()(documentation)来明确地设置地图位置源.

我在这里报告一个这样做的示例活动:

import com.google.android.gms.common.ConnectionResult;
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;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends FragmentActivity
    implements
        ConnectionCallbacks,OnConnectionFailedListener,LocationSource,LocationListener,OnMyLocationButtonClickListener,OnMapReadyCallback {

    private GoogleApiClient mGoogleApiClient;
    private TextView mMessageView;
    private OnLocationChangedListener mMapLocationListener = null;

    // location accuracy settings
    private static final LocationRequest REQUEST = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMessageView = (TextView) findViewById(R.id.message_text);

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mGoogleApiClient.connect();
    }

    @Override
    public void onPause() {
        super.onPause();
        mGoogleApiClient.disconnect();
    }

    @Override
    public void onMapReady(GoogleMap map) {
        map.setLocationSource(this);
        map.setMyLocationEnabled(true);
        map.setOnMyLocationButtonClickListener(this);
    }

    public void showMyLocation(View view) {
        if (mGoogleApiClient.isConnected()) {
            String msg = "Location = "
                    + LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Implementation of {@link LocationListener}.
     */
    @Override
    public void onLocationChanged(Location location) {
        mMessageView.setText("Location = " + location);
        if (mMapLocationListener != null) {
            mMapLocationListener.onLocationChanged(location);
        }
    }


    @Override
    public void onConnected(Bundle connectionHint) {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient,this);  // LocationListener
    }


    @Override
    public void onConnectionSuspended(int cause) {
        // Do nothing
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Do nothing
    }

    @Override
    public boolean onMyLocationButtonClick() {
        Toast.makeText(this,"MyLocation button clicked",Toast.LENGTH_SHORT).show();
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
        return false;
    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mMapLocationListener = onLocationChangedListener;
    }

    @Override
    public void deactivate() {
        mMapLocationListener = null;
    }
}
原文链接:/android/311990.html

猜你在找的Android相关文章