从 Google 个地点结果中获取经度和纬度
Get Longitude and Latitude from a Google Places Result
我正在使用 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 地图 API 上使用它的任何方法。我尝试通过调用 GetPlaceById:
来查看是否可以获取更多信息
var place = PlacesClass.GeoDataApi.GetPlaceById(googleApiClient, item.PlaceId);
但我在该结果中根本没有看到任何位置信息。有谁知道如何从 Google 地点 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.
略有不同
感谢所有帮助过的人。
我用过这个,你可以试试这个代码。
我正在使用 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,您可以检索纬度和经度,例如:latlngDrop.getLatitude、latlngDrop.getLongitude
我正在使用 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 地图 API 上使用它的任何方法。我尝试通过调用 GetPlaceById:
来查看是否可以获取更多信息var place = PlacesClass.GeoDataApi.GetPlaceById(googleApiClient, item.PlaceId);
但我在该结果中根本没有看到任何位置信息。有谁知道如何从 Google 地点 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.
略有不同感谢所有帮助过的人。
我用过这个,你可以试试这个代码。 我正在使用 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,您可以检索纬度和经度,例如:latlngDrop.getLatitude、latlngDrop.getLongitude