【问题标题】:Builds the map only when the GoogleApiClient is successfully connected?仅当 GoogleApiClient 连接成功时才构建地图?
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 为什么在本教程中developers.google.com/maps/documentation/android-api/… 开发人员展示了构建地图时没有建立 google play 服务连接(它不会显式调用 mGoogleApiClient.connect(); )?
    • 在本教程中,developers.google.com/maps/documentation/android-api/... 开发人员会显示地图并添加标记,但不会建立 google api 客户端连接(它不会显式调用mGoogleApiClient.connect();) 。我想appi客户端连接只是为了获取位置,还是我想错了?但是我已经在第一篇文章中编辑了这个问题,也许现在更清楚了:)
    • 没有你的假设是错误的,它是加载地图本身所必需的。
    • 为什么在某些教程中没有 googleapiclient 连接?例如在此代码中:developers.google.com/maps/documentation/android-api/…。 api客户端不是一直都需要吗?
    猜你喜欢
    • 1970-01-01
    • 2020-02-27
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多