我想在原生谷歌地图应用中实现搜索工具栏。我该怎么做?可能有本地方式来实现这个功能吗?
答案 0 :(得分:14)
您可以使用EditText
或TextWatcher
。我使用了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>
答案 1 :(得分:10)
如果您想查看Google地图的搜索视图。然后你可以使用以下两个很棒的库。
将以下依赖项添加到您的gradle文件中:
compile 'com.github.arimorty:floatingsearchview:2.0.3'
示例:
<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"/>
将以下依赖项添加到您的gradle文件中:
compile 'com.lapism:searchview:4.0'
示例:
<com.lapism.searchview.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
答案 2 :(得分:7)
您应该使用AutoSuggestEditext并提供像Google这样的背景效果,并按Relativelayout
以下是地方搜索的自动建议示例。
希望你能理解。
答案 3 :(得分:1)
这可能有助于想要添加搜索栏以及Google地图Google Places API
的人