Android:电池使用量低的谷歌地图位置 [英] Android: Google Maps location with low battery usage

本文介绍了Android:电池使用量低的谷歌地图位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用目前正在使用 Google Play Services

My app is currently using Maps by Google Play Services

指定:

mMap.setMyLocationEnabled(true);

每次在我的应用中显示地图时我都意识到:

I realize each time I am displaying the map in my app:

  • 位置在地图上用蓝点表示
  • 位置图标显示在顶部栏中
  • 如果我进入手机的设置/位置",我的应用会被报告为电池使用率高"

但是,我可以看到有些应用使用地图并仍然显示位置蓝点,但位置图标没有出现在顶部栏中并且它们的电池使用量很低.

However, I can see there are apps that use Maps and still show the location blue dot, but the location icon doesn't appear in top bar and their battery usage is low.

我的应用目前授予这两种权限:

My app currently grants both permissions:

  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_FINE_LOCATION

我的问题是:

如何在电量不足的情况下显示位置蓝点?

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

is it possible to specify the accuracy/battery usage by code?

更新

其实我意识到这样做的方法是使用GoogleApiClientFusedLocationApi

Actually I realized that the way to do it is to use the GoogleApiClient's FusedLocationApi

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

我已经在我的 Activity 中配置了 GoogleApiClient,调用:

I have configured the GoogleApiClient inside my Activity, calling:

  • GoogleApiClient.connect() 在 Activity 开始时
  • GoogleApiClient.disconnect() 在 Activity 停止
  • GoogleApiClient.connect() on the Activity's start
  • GoogleApiClient.disconnect() on the Activity's stop

onConnected 回调中,我设置了位置更新的标准:1 分钟的最快间隔,低功耗优先:

on the onConnected callback I set the criteria for the location updates: fastest interval of 1 minute with low power priority:

    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 的片段时,我的应用程序的设置/位置"仍然电池使用率高屏幕!

I have tested that the GoogleApiClient connects correctly at start, but for some reasons whenever I visit the fragment with the embedded MapView, I still get the high battery use for my app on the Settings/Location screen!

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

It seems the MapView is ignoring these low power criterias!

推荐答案

终于找到了解决方案!!!感谢 Tristan 的回答!

FINALLY FOUND THE SOLUTION!!! thanks to Tristan for his answer!

默认情况下,GoogleMap 使用其on location provider,而不是Fused Location Provider.为了使用 Fused Location Provider(它允许您控制位置准确性和功耗),您需要使用 GoogleMap.setLocationSource()(文档)

By default, GoogleMap uses its on location provider, which is not the Fused Location Provider. In order to use the Fused Location Provider (which allows you to control the location accuracy and power consumption) you need to explicitely set the map location source with GoogleMap.setLocationSource() (documentation)

我在这里报告一个示例活动来做到这一点:

I am reporting here a sample activity to do that:

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,
                REQUEST,
                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:电池使用量低的谷歌地图位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆