【问题标题】:Get Longitude and Latitude from a Google Places Result从 Google 地方信息结果中获取经度和纬度
【发布时间】:2018-06-26 23:10:18
【问题描述】:

我正在使用 Xamarin 编写一个 Android 应用程序,该应用程序使用 Xamarin.GooglePlayServices.Maps Nuget 包。我需要使用该地点的名称到达地图上的特定地点。为此,我加载了 Xamarin.GooglePlayServices.Places。我可以将部分地名传递给 GetAutocompletePredictionsAsync,如下所示:

var autocompletePredictions = await 
PlacesClass.GeoDataApi.GetAutocompletePredictionsAsync(
                Adapter.googleApiClient, constraint.ToString(), 
                Adapter.bounds, Adapter.autoCompleteFilter);

我得到的结果基本上是IAutocompletePrediction的集合。此对象中唯一与位置有关的项目是 PlaceId。我找不到在 Google Maps API 上使用它的任何方法。我尝试通过调用 GetPlaceById 来查看是否可以获取更多信息:

var place = PlacesClass.GeoDataApi.GetPlaceById(googleApiClient, item.PlaceId);

但我在该结果中根本没有看到任何位置信息。有谁知道如何从 Google Places API 获取 LatLng 信息?

更新:我使用了几个回复中的信息来获得答案:

            Task.Run(async () =>
            {
                PlaceBuffer places = await PlacesClass.GeoDataApi.GetPlaceByIdAsync(googleApiClient, item.PlaceId);

                if (places.Status.IsSuccess)
                {

                    foreach (var place in places)
                        try
                        {
                            //IPlace place = (IPlace)places.Get(0);

                            MarkOnMap(place.NameFormatted.ToString(), place.AddressFormatted.ToString(), place.LatLng);
                            UpdateCameraPosition(place.LatLng);
                        }
                        catch (Exception ex)
                        {
                            Log.Error("WhatNow", ex.ToString());
                        }
                }

                places.Dispose();
            });

奇怪的是,当我在 foreach 中列举地点时,地点是 IPlace 类型。不过

IPlace place = (IPlace)places.Get(0);

没有投射到 IPlace。谁知道?我只希望 Dispose() 按照文档中的建议释放缓冲区。 Xamarin 总是与 Java 略有不同。

感谢所有帮助过的人。

【问题讨论】:

  • 你试过.getLatLng() 吗? place 对象应该具有该功能。
  • 也只是为了确保我们在同一页面上。您问题中的结果是待处理的结果,以获取实际的 Place 对象 - developers.google.com/android/reference/com/google/android/gms/…
  • 尝试使用PlaceBuffer places = await PlacesClass.GeoDataApi.GetPlaceByIdAsync(googleApiClient, placeId)IPlace place = places.FirstOrDefault()place.AddressFormatted.ToString()

标签: android google-maps google-maps-api-3 xamarin.android google-places-api


【解决方案1】:

我用过这个,你可以试试这个代码。 我正在使用 autoCompleteTextView。

dropLocationEt.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final PlaceAutoCompleteAdapter.PlaceAutocomplete item = mAdapter.getItem(position);
                final String placeId = String.valueOf(item.placeId);
                Log.i(TAG, "Autocomplete item selected: " + item.description);
            /*
             Issue a request to the Places Geo Data API to retrieve a Place object with additional
              details about the place.
              */
                PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                        .getPlaceById(googleApiClient, placeId);
                placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
                    @Override
                    public void onResult(PlaceBuffer places) {
                        if (!places.getStatus().isSuccess()) {
                            // Request did not complete successfully
                            Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
                            places.release();
                            return;
                        }
                        // Get the Place object from the buffer.
                        final Place place = places.get(0);
                        latLngDrop = place.getLatLng();
                        StaticMethods.hideKeyboard(getActivity());
                        dropLocationEt.setSelection(0);

                    }
                });
            }
        });

从 latlngDrop 中,您可以检索 lat 和 lng,例如:latlngDrop.getLatitude, latlngDrop.getLongitude

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多