Android的使用PlaceAutoCompleteActivity(谷歌Places API的) [英] Android using PlaceAutoCompleteActivity (Google Places API)
问题描述
我已经我与 PlaceAutoCompelteActivity
遇到的问题的一个很简单的例子。我的地图上会显示在屏幕上,与上面的两个文本搜索栏一起。问题是,一旦我在TextView的点击,键盘会弹出,光标将在TextView中闪烁(如预期),但之后我键入的第一个字母(如果我快速输入我可以得到2个字母)时,键盘就会消失,搜索框会一片空白,光标就会消失(返回到pclicked状态的$ p $)。下面是该文档的链接:
https://developers.google.com/places/android-api/autocomplete ?HL = EN
在code为简单的活动和XML文件是如下:
Java的活动:
包com.blah.android.backseatbuddy;进口android.support.v4.app.FragmentActivity;
进口android.os.Bundle;
进口android.util.Log;进口com.google.android.gms.common.api.Status;
进口com.google.android.gms.location.places.Place;
进口com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
进口com.google.android.gms.location.places.ui.PlaceSelectionListener;
进口com.google.android.gms.maps.CameraUpdateFactory;
进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.OnMa preadyCallback;
进口com.google.android.gms.maps.SupportMapFragment;
进口com.google.android.gms.maps.model.LatLng;
进口com.google.android.gms.maps.model.MarkerOptions;公共类RouteActivity扩展FragmentActivity实现OnMa $ P $ {padyCallback 私人GoogleMap的MMAP;
//私人的EditText beginningDestination;
//私人的EditText endingDestination;
PlaceAutocompleteFragment beginningDestination;
PlaceAutocompleteFragment endingDestination; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_route); beginningDestination =(PlaceAutocompleteFragment)getFragmentManager()findFragmentById(R.id.beginning_destination)。
beginningDestination.setOnPlaceSelectedListener(新PlaceSelectionListener(){
@覆盖
公共无效onPlaceSelected(地点的地方){
// TODO:获取选定的地点信息。
Log.i(beginningDestination,地方:+ place.getName());
} @覆盖
公共无效onerror的(状态的状态){
// TODO:处理错误。
Log.i(beginningDestination,出现错误:+状态);
}
}); endingDestination =(PlaceAutocompleteFragment)getFragmentManager()findFragmentById(R.id.ending_destination)。
endingDestination.setOnPlaceSelectedListener(新PlaceSelectionListener(){
@覆盖
公共无效onPlaceSelected(地点的地方){
// TODO:获取选定的地点信息。
Log.i(endingDestination,地方:+ place.getName());
} @覆盖
公共无效onerror的(状态的状态){
// TODO:处理错误。
Log.i(endingDestination,出现错误:+状态);
}
});
// beginningDestination =(EditText上)findViewById(R.id.beginning_destination);
// endingDestination =(EditText上)findViewById(R.id.ending_destination); //获取SupportMapFragment并在地图就可以使用了得到通知。
SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(本);
}
@覆盖
公共无效onMa pready(GoogleMap的GoogleMap的){
MMAP = GoogleMap的; //在悉尼添加标记和移动相机
经纬度悉尼=新的经纬度(-34,151);
mMap.addMarker(新的MarkerOptions()位置(悉尼).title伪(标记在悉尼));
mMap.moveCamera(CameraUpdateFactory.newLatLng(悉尼));
}
}
和XML布局文件:
<?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID / relativeLayoutFragment
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> <片段
机器人:ID =@ + ID / beginning_destination
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:名字=com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
/> <片段
机器人:ID =@ + ID / ending_destination
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:名字=com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
/> <片段
机器人:ID =@ + ID /图
机器人:名字=com.google.android.gms.maps.SupportMapFragment
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:地图=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=com.greg.android.backseatbuddy.RouteActivity/>< / LinearLayout中>
您可能需要插入自己的API密钥(如果你还没有这样做的话),可以通过谷歌开发者控制台进行收购。
详细信息在这里:
https://developers.google.com/places/android-api/signup ?HL = EN
此外,在谷歌开发者控制台,添加谷歌Places API所为启用API的名单在您的项目。
I have made a very simple example of the issue I am having with the PlaceAutoCompelteActivity
. My map will show on the screen, along with 2 text search bars on top. The issue is that once I click in the textview, a keyboard will come up and a cursor will blink in the textview (as expected), however after I type the first letter (if i type quickly I can get 2 letters in), the keyboard will go away and the search box will blank out and the cursor will go away (return to the preclicked on state.) Here is a link to the documentation:
https://developers.google.com/places/android-api/autocomplete?hl=en
The code for the simple activity and the xml file are below:
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));
}
}
and xml layout file:
<?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>
You may need to insert your own API key (if you haven't done so already) that can be acquired through the Google developers console. Details here: https://developers.google.com/places/android-api/signup?hl=en Also, in the Google developers console, add Google Places API to the list of enabled API's in your project.
这篇关于Android的使用PlaceAutoCompleteActivity(谷歌Places API的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!