Android map.setMyLocationEnabled(true) 到底做了什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22519847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:06:47  来源:igfitidea点击:

What does map.setMyLocationEnabled(true) really do

androidgoogle-mapsgeolocationgoogle-maps-android-api-2

提问by lostintranslation

I am setting:

我正在设置:

map.setMyLocationEnabled(true)

But I am wondering what this really does. What I know:

但我想知道这到底是做什么的。我知道的:

  1. I get a locate me button in the upper right corner of the map
  2. I get a blue icon on the map that represents my current location
  1. 我在地图的右上角有一个定位按钮
  2. 我在地图上看到一个蓝色图标,代表我当前的位置

Here is my concern. I am writing a location aware app but I am concerned about battery. Elsewhere in my app (via preferences) I set up a LocationManager and listen for location events so I can log them. But part of my preferences is the accuracy at which I get updates and the interval.

这是我的担忧。我正在编写一个位置感知应用程序,但我担心电池问题。在我的应用程序的其他地方(通过首选项),我设置了一个 LocationManager 并监听位置事件,以便我可以记录它们。但我的部分偏好是我获得更新的准确性和间隔。

When I turn off my LocationManager:

当我关闭我的 LocationManager 时:

locationManange.removeUpdates(LocationListener listener);

I expect to turn off location services for the entire app. Yet I still see the little GPS icon in my phones header bar indicating the app is getting location updates. I know that is coming from the fact that I set this on my map view:

我希望关闭整个应用程序的位置服务。但是我仍然在我的手机标题栏中看到小 GPS 图标,表明该应用程序正在获取位置更新。我知道这是因为我在地图视图上设置了它:

map.setMyLocationEnabled(true);

I originally thought that if you did not listen for Location updates by setting up a LocationManager that set map.setMyLocationEnabled(true) would have no effect (ie no my location icon, or button). However that is not true.

我最初认为,如果您没有通过设置一个 LocationManager 来监听位置更新,则设置 map.setMyLocationEnabled(true) 将无效(即没有我的位置图标或按钮)。然而事实并非如此。

Does that mean that by calling map.setMyLocationEnabled(true) google is setting up its own LocationManager with its own settings? I would really like to call map.setMyLocationEnabled(true), but have it use my settings for location updates not whatever google is doing under the hood. Is that possible? Did I miss something in the docs? I cannot find anything about the accuracy or interval that is setup on a LocationManager when I call map.setMyLocationEnabled(true).

这是否意味着通过调用 map.setMyLocationEnabled(true) 谷歌正在使用自己的设置设置自己的 LocationManager ?我真的很想调用 map.setMyLocationEnabled(true),但让它使用我的设置进行位置更新,而不是谷歌在幕后所做的任何事情。那可能吗?我错过了文档中的某些内容吗?当我调用 map.setMyLocationEnabled(true) 时,我找不到有关在 LocationManager 上设置的准确性或间隔的任何信息。

采纳答案by CommonsWare

Does that mean that by calling map.setMyLocationEnabled(true) google is setting up its own LocationManager with its own settings?

这是否意味着通过调用 map.setMyLocationEnabled(true) 谷歌正在使用自己的设置设置自己的 LocationManager ?

Not exactly. Maps V2 uses LocationClientby default.

不完全是。Maps V2默认使用LocationClient

I would really like to call map.setMyLocationEnabled(true), but have it use my settings for location updates not whatever google is doing under the hood. Is that possible?

我真的很想调用 map.setMyLocationEnabled(true),但让它使用我的设置进行位置更新,而不是谷歌在幕后所做的任何事情。那可能吗?

You can use setLocationSource()to supply your own location data for use with the my-location layer. Here is a sample projectdemonstrating this.

您可以使用setLocationSource()提供您自己的位置数据以用于 my-location 图层。这是一个示例项目,演示了这一点。