places.getLatLng() 返回 null 虽然 places.getName() 没有返回 null
Posted
技术标签:
【中文标题】places.getLatLng() 返回 null 虽然 places.getName() 没有返回 null【英文标题】:places.getLatLng() returning null though places.getName() is not returning null 【发布时间】:2019-08-21 00:36:14 【问题描述】:我一直在尝试从自动完成中单击一个位置后获取 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());
我希望以下代码返回我在自动完成活动中选择的地点的纬度和对数
【问题讨论】:
确保您已启用位置设置 位置设置已启用 【参考方案1】:以前,Places SDK for android 是通过 Google Play 服务提供的。 Places SDK for Android 的 Google Play 服务版本已弃用,Google 将在 2019 年 7 月 29 日之前将其关闭。
但是,新版 Places SDK for Android 是作为静态客户端库分发的。 新 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));
For more details related to Place Data fields please refer here
【讨论】:
谢谢我没有提到 Place.Field.LAT_LNG,非常感谢您的帮助!你是救星【参考方案2】:我遇到了同样的问题,最后我知道我们需要请求Place.Field.LAT_LNG
。
此功能会自动完成
private void openGetLocation()
// Set the fields to specify which types of place data to
// return after the user has made a selection.
List<Place.Field> fields = Arrays.asList(Place.Field.LAT_LNG,Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields)
.build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
在此处获取地点详情onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1)
Toast.makeText(this, data.toString(), Toast.LENGTH_SHORT).show();
Place place = Autocomplete.getPlaceFromIntent(data);
locationEd.setText(place.getLatLng().toString());
else if (resultCode == AutocompleteActivity.RESULT_ERROR)
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(this, status.getStatusMessage(), Toast.LENGTH_SHORT).show();
注意:
这里我请求了三个东西Place.Field.LAT_LNG
,Place.Field.ID
,Place.Field.NAME
【讨论】:
以上是关于places.getLatLng() 返回 null 虽然 places.getName() 没有返回 null的主要内容,如果未能解决你的问题,请参考以下文章
为啥 environment.getProperty("spring.profiles.active") 在使用 @ActiveProfiles 激活测试中的配置文件时返回 nul
如果在 User.Identity.IsAuthenticated 为 false 时提供两个条件,GetUser(string userName, bool isOnline) 是不是仍返回 nul