如何在地图上显示地点可能性?

问题描述 投票:1回答:1
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"));
java android google-maps google-places-api google-places
1个回答
0
投票

将获得的经度/纬度另存为变量,例如:

latLng = placeLikelihood.getPlace().getLatLng()

然后按如下所示将其传递到标记中:

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

资源:https://developers.google.com/places/android-sdk/current-place#get-currenthttps://developers.google.com/maps/documentation/android-sdk/map-with-marker#add_a_map

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.