gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:17:29 37 4
gpt4 key购买 nike

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

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/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com