Java Android使用PlaceAutoCompleteTactivity(谷歌PlaceAPI)
我举了一个非常简单的例子来说明我在Java Android使用PlaceAutoCompleteTactivity(谷歌PlaceAPI),java,android,google-maps,google-places-api,Java,Android,Google Maps,Google Places Api,我举了一个非常简单的例子来说明我在placeautocomperteactivity中遇到的问题。我的地图将显示在屏幕上,顶部有两个文本搜索栏。问题是,一旦我在文本视图中单击,键盘就会出现,光标会在文本视图中闪烁(如预期),但在我键入第一个字母后(如果我快速键入,我可以输入两个字母),键盘将消失,搜索框将空白,光标将消失(返回到预单击打开状态)以下是文档的链接: 简单活动和xml文件的代码如下: java活动: package com.blah.android.backseatbuddy;
placeautocomperteactivity
中遇到的问题。我的地图将显示在屏幕上,顶部有两个文本搜索栏。问题是,一旦我在文本视图中单击,键盘就会出现,光标会在文本视图中闪烁(如预期),但在我键入第一个字母后(如果我快速键入,我可以输入两个字母),键盘将消失,搜索框将空白,光标将消失(返回到预单击打开状态)以下是文档的链接:
简单活动和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>
您可能需要插入自己的API密钥(如果您还没有这样做),该密钥可以通过Google开发者控制台获得。 详情如下:
另外,在Google开发者控制台中,将Google Places API添加到项目中已启用的API列表中。我的API密钥已经获得并正确配置,否则地图和搜索栏将不会显示在alli上。抱歉,您的答案实际上非常接近需要做的事情。我已经检索并将项目的api密钥插入到相应的(google_maps_api.xml)文件中,但我还没有将google places android api添加到google开发者控制台中我的“enabled api”部分(maps android api已经存在,这就是为什么地图可以工作的原因)。如果你想更新你的答案,我很乐意接受。我编辑了我的答案。:-)如果仍然不完整,请告诉我。谢谢同时启用后,问题仍然存在!help meI刚刚运行了您的代码,无法再现该问题。在带有安卓6.0的Nexus6上运行。1@DanielNugent hm:那么自动完成工作正常还是代码没有运行?我使用的是带Android 5.0的Galaxy S5。代码对我来说运行正常,没有任何问题。我能够在两个PlaceAutocompleteFragment字段中键入位置。@Daniel感谢您对我的问题的关注,但是您的评论以及felix尝试的回答让我找到了解决方案。见下面菲利克斯的答案