仅在成功连接GoogleApiClient时构建地图?

时间:2017-01-17 08:17:55

标签: android google-maps geolocation google-maps-markers google-maps-android-api-2

我正在开发一款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....
    }

1 个答案:

答案 0 :(得分:1)

只有启用了Google Play服务连接后,地图才会加载到设备中,如果地图尚未加载,则无法绘制标记。因此,如果没有Google Play,则无法构建地图服务。