Как отобразить вероятности места на карте? - PullRequest
1 голос
/ 27 января 2020
List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.LAT_LNG);


// Use the builder to create a FindCurrentPlaceRequest.

    FindCurrentPlaceRequest request =
            FindCurrentPlaceRequest.newInstance(placeFields);

// Call findCurrentPlace and handle the response (first check that the user has granted permission).

    if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
        placeResponse.addOnCompleteListener(task -> {
            if (task.isSuccessful()){
                FindCurrentPlaceResponse response = task.getResult();
                for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {

                    Log.i("TAG", String.format("Place '%s' has likelihood: %f",
                            placeLikelihood.getPlace().getName(),
                            placeLikelihood.getLikelihood()));
                }
            } else {
                Exception exception = task.getException();
                if (exception instanceof ApiException) {
                    ApiException apiException = (ApiException) exception;
                    Log.e(TAG, "Place not found: " + apiException.getStatusCode());
                }
            }
        });
    } else {

        // A local method to request required permissions;
        // See https://developer.android.com/training/permissions/requesting

        Log.i("TAG", "Turn on location on your device!");
    }

Я хочу отобразить все эти места на карте с помощью маркера или чего-либо альтернативно существующего:

mMap.addMarker(new MarkerOptions().position(currentLoc).title("Marker in Birmingham"));

1 Ответ

1 голос
/ 29 января 2020

Сохраните полученный лат / lng как переменную, например:

latLng = placeLikelihood.getPlace().getLatLng()

Затем передайте его в маркер следующим образом:

mMap.addMarker(new MarkerOptions().position(latLng))

Ресурсы:
https://developers.google.com/places/android-sdk/current-place#get -поток
https://developers.google.com/maps/documentation/android-sdk/map-with-marker#add_a_map

Надеюсь, это поможет!

...