本文介绍了尽管Places.getName()没有返回Null,但返回Null的Places.getLatLng()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试在从AutoComplete中单击一个位置后获得延迟。奇怪的是,places.getName()
工作正常,但place.getLatLng()
返回空值。我应该怎么做才能解决这个问题?我是Google Maps and Places 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());
我希望下面的代码返回我在自动完成活动中选择的位置的纬度和对数
推荐答案
之前,Places SDK for Android是通过Google Play服务提供的。Google Play Services版本的Places SDK for Android已弃用,Google将在2019年7月29日将其关闭。
但是,新版本的Places SDK for Android是作为静态客户端库分发的。
新SDK客户端库的依赖项
implementation 'com.google.android.libraries.places:places:1.0.0'
您可以使用以下两种方式之一在Android应用程序中添加Google Places自动完成:
1) Either embed a AutocompleteSupportFragment
OR
2) Use an intent to launch the autocomplete activity.
我通过嵌入以下AutoComplete SupportFragment创建了一个演示应用程序:-
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()+"
"+place.getId()+"
"+ 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));
For more details related to Place Data fields please refer here
这篇关于尽管Places.getName()没有返回Null,但返回Null的Places.getLatLng()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!