java - Android 使用 PlaceAutoCompleteActivity (Google Places API)

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

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

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

Java Activity :

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>

最佳答案

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

关于java - Android 使用 PlaceAutoCompleteActivity (Google Places API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582898/

相关文章:

java - 二进制运算 : why hash%n is equivalent to hash&(n-1)?

java - 带有 H2 文件数据库的 Spring Boot 应用程序

java - 查询Google Drive文件夹并更改共享权限

android - Spring for Android 有用吗?

javascript - 从 .gpx 文件导入坐标并使用 Google Maps API 显示的最佳方式

javascript - Google Maps API - 沿纬度/经度之间的路线获取点

java - 我想要java中两个日期之间的差异?

Android 自定义 header

android - ActionBar 中的 Logo 左对齐

javascript - 如何检查两条路由是否共享路径