Google地图中的自动完成搜索栏 [英] AutoComplete Search bar in Google Maps

查看:147
本文介绍了Google地图中的自动完成搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建基于地方搜索的应用。我想知道如何将搜索栏添加到我的Google地图中,用户可以在其中选择地点,并且可以捕捉用户选择的内容。



我尝试在用户界面中显示在Google地图上方显示的自动完成搜索框。 您可以简单地使用一个



当您点击自动完成栏,它将如下所示:





然后,开始输入,然后选择一个地方:



当你点击一个地方选择它时,你会看到来自PlaceSelectionListener的日志:

  D /地图:选择地点:旧金山


I'm creating an app based on places search. I would like to know how can I add a search bar into my Google Map, where the user can select a Place, and I can capture what the user chose.

I'm trying to include an autocomplete Search Box that is displayed above the Google Map in the UI.

解决方案

You can simply use a PlaceAutoCompleteFragment.

First ensure that you're using the latest version of Google Play Services (Version 8.4.0 and up includes the PlaceAutoCompleteFragment class):

compile 'com.google.android.gms:play-services-maps:11.0.2'
compile 'com.google.android.gms:play-services-location:11.0.2'
compile 'com.google.android.gms:play-services-places:11.0.2'

Then include the PlaceAutoCompleteFragment in your xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <fragment
        android:id="@+id/place_autocomplete"
        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"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

Then set up a listener in your Activity:

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;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    PlaceAutocompleteFragment placeAutoComplete;

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

        placeAutoComplete = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete);
        placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {

                Log.d("Maps", "Place selected: " + place.getName());
            }

            @Override
            public void onError(Status status) {
                Log.d("Maps", "An error occurred: " + status);
            }
        });

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

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

When you run this code, you'll see the AutoComplete bar above the Google Map:

When you click on the AutoComplete bar, it will look like this:

Then, start typing, and select a Place:

When you tap on a place to select it, you will see the log from the PlaceSelectionListener:

D/Maps: Place selected: San Francisco

这篇关于Google地图中的自动完成搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆