Google的GetCurrentPlace映射到ListView

时间:2016-03-13 11:29:45

标签: android google-maps listview

我获取附近地方的信息,并希望制作他们的列表,但它不起作用,我尝试做适配器,在列表中列出保存数据并在适配器中设置,你可以在代码中看到它。请告诉我我该怎么做?

放置检测:

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
            .getCurrentPlace(mGoogleApiClient, null);
    result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
        @Override
        public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
            for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                Log.i(TAG, String.format("Place '%s' has likelihood: %g",
                        placeLikelihood.getPlace().getName(),
                        placeLikelihood.getLikelihood()));
                        listForAd.add(String.valueOf(placeLikelihood)); //it how i try to set list, but not work.


            }
            likelyPlaces.release();
        }
    });

我尝试的适配器:

 final ArrayList<String> listForAd = new ArrayList<>(); //create list,
 // I try to write the data above it

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,listForAd );
        listView.setAdapter(adapter);

Log中的数据:

 03-13 13:18:30.982 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place       'ПП ОХОРОННЕ АГЕНТСТВО ЩИТ-2' has likelihood: 0,100000
03-13 13:18:30.983 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Спортивно-оздоровительный комплекс "Олимпийский"' has likelihood: 0,100000
03-13 13:18:30.984 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ТОВ А.Д.С.' has likelihood: 0,100000
03-13 13:18:30.985 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ул. Олимпийская' has likelihood: 0,0500000
03-13 13:18:30.987 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Надежда' has likelihood: 0,0500000
03-13 13:18:30.988 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Михайлик л. Н., част. Предпр-тель' has likelihood: 0,0500000
03-13 13:18:30.988 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Межшкольный Учебно-производственный Комбинат №5 Фрунз. р-на' has likelihood: 0,00000
03-13 13:18:30.989 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ул. Олимпийская' has likelihood: 0,00000
03-13 13:18:30.989 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place '5-я Городская Поликлиника' has likelihood: 0,00000
03-13 13:18:30.990 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ул. Ковтуна' has likelihood: 0,00000
03-13 13:18:30.990 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Верона, Лтд, Ф., ООО' has likelihood: 0,00000
03-13 13:18:30.991 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Аналитика' has likelihood: 0,00000
03-13 13:18:30.991 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ЧП Панченко Д.С.' has likelihood: 0,00000
03-13 13:18:30.991 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ООО "Баланс"' has likelihood: 0,00000
03-13 13:18:30.993 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ул. Олимпийская' has likelihood: 0,00000
03-13 13:18:30.993 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Отель SV Park Hotel' has likelihood: 0,00000
03-13 13:18:30.994 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Маршал' has likelihood: 0,00000
03-13 13:18:30.994 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'ул. Олимпийская' has likelihood: 0,00000
03-13 13:18:30.994 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'Перспектива, Потребительский Кооператив' has likelihood: 0,00000
03-13 13:18:30.994 8950-8950/com.dmitriy.azarenko.thisifbars I/tag: Place 'электроинструмент' has likelihood: 0,00000

1 个答案:

答案 0 :(得分:0)

Google PlaceDetectionApi可以通过自动检测您的位置来获取您周围的地方列表。所以您必须发送请求以获取下面附近地点的列表

private void callPlaceDetectionApi() {
        Log.d(TAG, "callPlaceDetectionApi() called");
        PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
                .getCurrentPlace(mGoogleApiClient, null);
        result.setResultCallback(this);
    }

在OnResult Callback中,您可以将此可能性位置添加到您自己的 NearbyPlace 模型类

@Override
    public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
        Log.d(TAG, "onResult() called with: " + "likelyPlaces = [" + likelyPlaces.toString() + "]");
        List<NearbyPlace> nearbyPlaces = new ArrayList<>();
        for (PlaceLikelihood placeLikelihood : likelyPlaces) {
            NearbyPlace nearbyPlace = new NearbyPlace();
            Log.i(TAG, String.format("Place '%s' with " +
                            "likelihood: %g",
                    placeLikelihood.getPlace().getName(),
                    placeLikelihood.getLikelihood()));
            nearbyPlace.setName(placeLikelihood.getPlace().getName().toString());
            nearbyPlace.setLikelihood(placeLikelihood.getLikelihood());
            nearbyPlace.setAddress(placeLikelihood.getPlace().getAddress().toString());
            nearbyPlaces.add(nearbyPlace);
        }

    }

我在我的博客How to retrieve List of Nearby Places using PlaceDetectionApi上写了一篇文章。