在谷歌地图上添加搜索工具栏,就像在原生Android应用程序中

And*_*w F 24 android google-maps

我想在本机谷歌地图应用程序中实现搜索工具栏.我该怎么做?可能有本地方式来实现此功能?

来自谷歌地图应用程序的快照

Jis*_*ant 14

你可以使用EditTextTextWatcher.我使用了一个EditText和一个搜索按钮.使用此按钮单击,它搜索位置.

这是我点击按钮的代码:

button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String g = searchview.getText().toString();

            Geocoder geocoder = new Geocoder(getBaseContext());
            List<Address> addresses = null;

            try {
                // Getting a maximum of 3 Address that matches the input
                // text
                addresses = geocoder.getFromLocationName(g, 3);
                if (addresses != null && !addresses.equals(""))
                    search(addresses);

            } catch (Exception e) {

            }

        }
    });
Run Code Online (Sandbox Code Playgroud)

这是搜索方法

protected void search(List<Address> addresses) {

    Address address = (Address) addresses.get(0);
    home_long = address.getLongitude();
    home_lat = address.getLatitude();
    latLng = new LatLng(address.getLatitude(), address.getLongitude());

    addressText = String.format(
            "%s, %s",
            address.getMaxAddressLineIndex() > 0 ? address
                    .getAddressLine(0) : "", address.getCountryName());

    markerOptions = new MarkerOptions();

    markerOptions.position(latLng);
    markerOptions.title(addressText);

    map1.clear();
    map1.addMarker(markerOptions);
    map1.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    map1.animateCamera(CameraUpdateFactory.zoomTo(15));
    locationTv.setText("Latitude:" + address.getLatitude() + ", Longitude:"
            + address.getLongitude());


}
Run Code Online (Sandbox Code Playgroud)

编辑 - 用于xml代码

<RelativeLayout 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="com.example.google_map.MainActivity" >

 <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    class="com.google.android.gms.maps.SupportMapFragment" />

<TextView
    android:id="@+id/latlongLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/searchView1"
    android:background="#ff058fff"
    android:gravity="bottom"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:textColor="#ffffffff" />

<EditText
    android:id="@+id/searchView1"
    android:layout_width="250dp"
    android:layout_height="34dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:layout_marginTop="14dp"
    android:hint="Search Location"
    android:textColor="@android:color/black"
    android:background="@android:color/white" >
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="28dp"
    android:layout_height="25dp"
    android:layout_alignBaseline="@+id/searchView1"
    android:layout_alignBottom="@+id/searchView1"
    android:layout_alignRight="@+id/searchView1"
    android:background="@drawable/g_search_btn" /></RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


Pri*_*tel 10

如果您想查看Google地图的搜索视图.然后你可以使用以下两个真棒库中的任何一个.

1)浮动搜索视图

将以下依赖项添加到您的gradle文件中:

compile 'com.github.arimorty:floatingsearchview:2.0.3'
Run Code Online (Sandbox Code Playgroud)

例:

   <com.arlib.floatingsearchview.FloatingSearchView
            android:id="@+id/floating_search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:floatingSearch_searchBarMarginLeft="@dimen/search_view_inset"
            app:floatingSearch_searchBarMarginTop="@dimen/search_view_inset"
            app:floatingSearch_searchBarMarginRight="@dimen/search_view_inset"
            app:floatingSearch_searchHint="Search..."
            app:floatingSearch_suggestionsListAnimDuration="250"
            app:floatingSearch_showSearchKey="false"
            app:floatingSearch_leftActionMode="showHamburger"
            app:floatingSearch_menu="@menu/menu_main"
            app:floatingSearch_close_search_on_keyboard_dismiss="true"/>
Run Code Online (Sandbox Code Playgroud)

2)SearchView

将以下依赖项添加到您的gradle文件中:

compile 'com.lapism:searchview:4.0'
Run Code Online (Sandbox Code Playgroud)

例:

<com.lapism.searchview.SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)


Her*_*rry 7

您应该使用AutoSuggestEditext并提供像google这样的背景效果并将左右图像放入 Relativelayout

以下是用于地点搜索的Auto Suggest的示例.

自动放置完成

希望你能理解.