android - places.getLatLng() 返回 null 虽然 places.getName() 没有返回 null
问题描述
在从自动完成中单击一个位置后,我一直在尝试获取 latlng,但奇怪的是它places.getName()
工作正常但place.getLatLng()
返回 null。我应该怎么做才能解决这个问题我是谷歌地图和地点 API 的新手!
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTOCOMPLETE_REQUEST_CODE_FROM) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i("shipadd", "Place: " + place.getName() + ", " + place.getId());
from_edit_txt.setText(place.getName());
origin = place.getLatLng();
Log.e("origin_destarray", "" + place.getLatLng());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i("shipadd", status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
} else if (requestCode == AUTOCOMPLETE_REQUEST_CODE_TO) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i("shipadd", "Place: " + place.getAddress() + ", " + place.getId());
to_edit_txt.setText(place.getName());
我希望以下代码返回我在自动完成活动中选择的地点的纬度和对数
解决方案
以前,适用于 Android 的 Places SDK 是通过 Google Play 服务提供的。 适用于 Android 的 Places SDK 的 Google Play 服务版本已弃用,Google 将在 2019 年 7 月 29 日之前将其关闭。
但是,Android 版 Places SDK 的新版本是作为静态客户端库分发的。
新 SDK 客户端库的依赖项
implementation 'com.google.android.libraries.places:places:1.0.0'
您可以使用在 android 应用程序中添加 Google 地方自动完成功能的两种方法之一:
1) Either embed a AutocompleteSupportFragment
OR
2) Use an intent to launch the autocomplete activity.
我通过在下面嵌入 AutocompleteSupportFragment 创建了一个演示应用程序:-
package com.places_api_demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.Arrays;
import com.google.android.gms.common.api.Status;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.net.PlacesClient;
import com.google.android.libraries.places.widget.AutocompleteSupportFragment;
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener;
import com.google.android.libraries.places.api.Places;
import static com.google.android.libraries.places.api.model.Place.Field.LAT_LNG;
public class MainActivity extends AppCompatActivity {
String TAG = "placeautocomplete";
TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = findViewById(R.id.txtView);
// Initialize Places.
Places.initialize(getApplicationContext(), "REPLACE YOUR API KEY HERE");
// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
txtView.setText(place.getName()+"\n"+place.getId()+"\n"+ place.getLatLng());
Log.i(TAG, "Place: " + place.getName() + ", " + place.getLatLng());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
}
}
指定要返回的地点数据的类型也很重要
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.LAT_LNG, Place.Field.ID, Place.Field.NAME));
推荐阅读
- python - 有效地将 sqlalchemy 记录到文件中?
- python-3.x - 对于跨越 DST 更改的日期,模糊参数在 df['datetime'].dt.tz_localize('America/Los_Angeles', ambiguous ='NaT') 中不起作用
- python - 从具有特定键值的字典列表中删除元素
- bash - 这个 bash 反向 shell 如何接受输入?
- google-chrome - 错误:找不到符号文件。默认为 ....\chrome.dll 导出符号
- c# - 在 iTextSharp 中的段落中添加自定义字体
- python - 根据多个条件对df进行排序
- c++ - 是否可以从原始大小的 OpenGL 缓冲区获取像素?
- powershell - 我怎样才能阻止这个用于批量重命名的power shell命令如此奇怪地增加?
- reactjs - React-Router:为什么react中未定义useHistory?