Google Places SDK在首次运行应用程序时加载速度较慢

时间:2019-02-08 04:11:21

标签: android performance google-places-api google-places app-startup

[背景]我有一个应用程序,该应用程序在数据库中具有商店列表,其商店ID保存在字段中。当用户输入其位置时,该应用程序应在数据库中的商店列表中显示最接近该用户的商店。因此,代码将遍历每个Shop Place ID,从Google Places获取纬度/经度,并计算与用户位置的距离。这是下面的代码片段:

 for (int position = 0; position < results.size(); position++) {
            // Get the placeID 
            String placeId = results.get(position).getAddress();

            // Specify the fields to return.
            List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME,
                    Place.Field.LAT_LNG, Place.Field.ADDRESS);

            // Construct a request object, passing the place ID and fields array.
            FetchPlaceRequest request = FetchPlaceRequest.builder(placeId, placeFields)
                    .build();

            // create a FetchPlaceResponse task
            Task<FetchPlaceResponse> task = placesClient.fetchPlace(request);

            try {
                // wait for the task to finish
                FetchPlaceResponse response = Tasks.await(task);
                Place place = response.getPlace();

                // Get the latitude and longitude for the place
                LatLng latLng = place.getLatLng();

                // Set the location object for the specific business
                Location A = new Location("Business");
                A.setLatitude(latLng.latitude);
                A.setLongitude(latLng.longitude);

                // get the distance of the business from the user location
                float distance = A.distanceTo(mUserLocation);

我在日志中注意到的是,在初次/首次运行期间,代码运行缓慢(大约7秒)。但是,成功的运行/请求仅需2秒钟即可完成。

所以我所做的是,在用户登录期间,我第一次使用虚拟的场所ID来运行Google场所。结果显然会失败,但是至少在使用真实地点ID的用户的第一次实际运行中,结果要快2秒。发生这种情况的任何原因?

0 个答案:

没有答案
相关问题