【问题标题】:How to display placelikelihood on map?如何在地图上显示地点可能性?
【发布时间】:2020-05-13 00:39:48
【问题描述】:
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】:

    将获得的 lat/lng 保存为变量,例如:

    latLng = placeLikelihood.getPlace().getLatLng()

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

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

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

    希望这会有所帮助!

    【讨论】:

    • 谢谢帮助,有点工作。我实际上需要的不仅仅是可能性的地方。我想在特定半径内按特定名称查找附近的地点,但新的 Places SDK for android 不允许我这样做。
    • Android 版 Places SDK 没有附近的搜索服务。您需要使用 Web 服务/Java 客户端,但请注意,用于附近搜索或其他 Web 服务的 API 密钥不会受到保护,您需要实施其他安全技术,例如设置代理服务器。
    • 我没有,但我可以自己实现它,然后向您展示我的代码,以便您可以将其用作指导。我可以看到您已经为此创建了一个问题,因此一旦我有工作代码,我将在该线程中回答您。但可能明天会这样做,因为我现在太累了哈哈。
    • 我将不胜感激。我真的需要帮助!干杯
    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多