I want to implement search toolbar like in native google maps app. How can I make this? May be there is native way to implement this feature?
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
As we are adding SearchView to our Google Maps for searching a location and adding a marker on that location. So we have to add a search view to our activity_maps.xml file. For adding SearchView, navigate to the app > res > layout > activity_maps.xml and add the below code to it. Comments are added in the code to get to know in more detail.
What you'll do 1 Enable and use the Maps SDK for Android to add Google Maps to an Android app. 2 Add, customize, and cluster markers. 3 Draw polylines and polygons on the map. 4 Control the viewpoint of the camera programmatically. More ...
Note: The nomenclature GoogleMap.add is a common way to draw on the map. If you're curious, you can explore all the different ways you can add functionality to the map by typing in .add on a GoogleMap object in Android Studio. This autocompletes all the different methods supported.
After generating your API key for Google Maps. We have to add this key to our Project. For adding this key in our app navigate to the values folder > google_maps_api.xml file and at line 23 you have to add your API key in the place of YOUR_API_KEY .
You can use EditText
or TextWatcher
. I used an EditText
and a Search button. With this on button click, it search location.
Here is my code on button click :
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) { } } });
and here is search method
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()); }
Edited- for xml code
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With