【发布时间】:2017-04-30 13:42:37
【问题描述】:
我一直在阅读 android 开发者文档,试图找出如何获取当前位置,特别是 https://developer.android.com/training/location/retrieve-current.html
当我在 android 中创建一个新项目并选择 Map 模板时,onCreate 方法中有代码,然后还有一个 onMapReady 方法。
首先,确认下面的代码是否将地图显示在屏幕上?如果是这样,onMapReady 只是一种方法,然后允许操纵地图吗?
SupportMapFragment mapFragment = (SupportMapFragment) etSupportFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);
在文档 re:get the current location 中有信息 re:building to the GoogleApiClient eg
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
文档中指定的其他方法是 onStart、onStop 和 onConnected。这一切都是有道理的,但要制作一个非常基本的获取当前位置应用程序,我是否仍然使用 SupportMapFragment mapFragment = (SupportMapFragment) .... 从 onCreate 中生成地图?还需要 onMapReady 函数吗?哪里是创建 GoogleApiClient 实例的最佳位置?
在随后的文档页面上,还有关于在连接后获取当前位置设置的信息
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
但我只是不确定应该在哪里定义。
最后在 Android Studio 的 Map 模板中,默认类定义为:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
但在谷歌文档中它被定义为:
公共类 MainActivity 扩展 ActionBarActivity 实现 ConnectionCallbacks, OnConnectionFailedListener
类扩展哪个 Activity 重要吗?
谢谢。
【问题讨论】:
标签: android google-maps google-play-services