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

标签 android google-maps

我想在本地谷歌地图应用中实现搜索工具栏。我怎样才能做到这一点?可能有实现此功能的本地方式吗?

snapshot from google maps app

最佳答案

您可以使用 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) {

            }

        }
    });

这里是搜索方法

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());


}

已编辑 - 用于 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>

关于android - 在谷歌地图上添加搜索工具栏,就像在原生 android 应用程序中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136527/

相关文章:

android - 在android中有Hue时查找颜色名称

java - 发送短信在 MyJavaScriptInterface 中不起作用 - Android Studio

javascript - Jquery 每个按值排序

android - TextView 不换行内容

java - null 对象引用上的 childEventListerer,Firebase 数据库

java - 改造解析 JSON 响应空值 Android

javascript - 谷歌地图中的 "You have exceeded your request quota for this API"

android - Google Maps v3 MapView 未在 Android 中获取所有父 View

javascript - Angular:谷歌地图第一次工作,然后仅在移动屏幕上显示空白

安卓全景 View