【发布时间】:2017-06-01 05:10:32
【问题描述】:
我正在开发一个用于锻炼的 Android 应用,以便更好地了解 Google 地图 api。在这个应用程序中,我想显示一些标记,如果 Android 知道用户的当前位置,我想在地图上显示它。按照谷歌教程,我做到了。但我对获取地图和建立 Google Api 客户端连接的更好方法存有疑问。在教程https://developers.google.com/maps/documentation/android-api/current-places-tutorial 中,开发人员仅在成功连接 Google Api 客户端后构建地图。在我的研究案例中这样做是否正确?在我的情况下,重要的是在地图上显示一些标记位置,并且仅当它可能还显示当前用户位置? 在我看来,我会以相反的方式做:在构建地图之前并且只有在地图准备好的回调中才会建立 GoogleApiClient 连接。 (我认为Api客户端连接只需要获取当前位置正确吗?)
在教程中他们是这样做的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
buildGoogleApiClient();
mGoogleApiClient.connect();
....
}
/**
* Builds a GoogleApiClient.
* Uses the addApi() method to request the Google Places API and the Fused Location Provider.
*/
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
this /* OnConnectionFailedListener */)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
createLocationRequest();
}
@Override
public void onConnected(Bundle connectionHint) {
getDeviceLocation();
// Build the map.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
*/
@Override
public void onMapReady(GoogleMap map) {
.... do stuff....}
我会按照以下方式完成:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
buildGoogleApiClient();
// Build the map.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Builds a GoogleApiClient.
* Uses the addApi() method to request the Google Places API and the Fused Location Provider.
*/
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
this /* OnConnectionFailedListener */)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
createLocationRequest();
}
@Override
public void onConnected(Bundle connectionHint) {
getDeviceLocation();
}
*/
@Override
public void onMapReady(GoogleMap map) {
mGoogleApiClient.connect();
----do stuff....
}
【问题讨论】:
标签: android google-maps geolocation google-maps-markers google-maps-android-api-2