【问题标题】:Android using PlaceAutoCompleteActivity (Google Places API)Android 使用 PlaceAutoCompleteActivity (Google Places API)
【发布时间】:2016-04-07 13:53:08
【问题描述】:

我已经就PlaceAutoCompelteActivity 遇到的问题做了一个非常简单的示例。我的地图将显示在屏幕上,顶部有 2 个文本搜索栏。问题是,一旦我在 textview 中单击,就会出现一个键盘,并且光标会在 textview 中闪烁(如预期的那样),但是在我输入第一个字母后(如果我快速输入,我可以输入 2 个字母),键盘将消失,搜索框将变为空白,光标将消失(返回到预单击状态。)这是文档的链接: https://developers.google.com/places/android-api/autocomplete?hl=en

简单活动的代码和xml文件如下:

java 活动:

package com.blah.android.backseatbuddy;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class RouteActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    //    private EditText beginningDestination;
//    private EditText endingDestination;
    PlaceAutocompleteFragment beginningDestination;
    PlaceAutocompleteFragment endingDestination;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route);

        beginningDestination = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.beginning_destination);
        beginningDestination.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i("beginningDestination ", "Place: " + place.getName());
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i("beginningDestination ", "An error occurred: " + status);
            }
        });

        endingDestination = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.ending_destination);
        endingDestination.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i("endingDestination ", "Place: " + place.getName());
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i("endingDestination ", "An error occurred: " + status);
            }
        });
//        beginningDestination = (EditText) findViewById(R.id.beginning_destination);
//        endingDestination = (EditText) findViewById(R.id.ending_destination);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

和xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/relativeLayoutFragment"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <fragment
        android:id="@+id/beginning_destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

    <fragment
        android:id="@+id/ending_destination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.greg.android.backseatbuddy.RouteActivity"/>

</LinearLayout>

【问题讨论】:

  • 我刚刚运行了您的代码,但无法重现该问题。在装有 Android 6.0.1 的 Nexus 6 上运行
  • @Daniel Nugent hm 所以自动完成工作正常还是代码没有运行?
  • 我使用的是搭载 Android 5.0 的 Galaxy S5
  • 代码对我来说运行得很好,没有任何问题。我能够在两个 PlaceAutocompleteFragment 字段中输入位置。
  • @Daniel 感谢您对我的问题的关注,但是您的评论以及 felix 的尝试回答让我找到了解决方案。见下文菲利克斯的回答

标签: java android google-maps google-places-api


【解决方案1】:

您可能需要插入您自己的 API 密钥(如果您还没有这样做的话),该密钥可以通过 Google 开发者控制台获取。 详情在这里: https://developers.google.com/places/android-api/signup?hl=en 此外,在 Google 开发者控制台中,将 Google Places API 添加到项目中启用的 API 列表中。

【讨论】:

  • 我的 api 密钥已经获取并正确配置,否则地图和搜索栏根本不会显示
  • 我很抱歉,您的回答实际上非常接近需要做的事情。我已经检索了我的项目的 api 密钥并将其插入到适当的 (google_maps_api.xml) 文件中,但我还没有将 google places android api 添加到我在 google 开发人员控制台中的“启用的 API”部分(地图 android api已经在那里,所以这就是地图工作的原因)。如果你想更新你的答案,我很乐意接受。
  • 我编辑了我的答案。 :-) 如果仍然不完整,请告诉我。谢谢!
  • 也启用后,问题依旧!帮帮我
猜你喜欢
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多