Kotlin Android Google Map Search Location
In the previous tutorial, we build the application that locate the Map Fixed Location and Map Current Location.
In this tutorial, we will implement search location functionality in Google Map. The searching of Google location is done through Geocoder class. Geocoder class facilitates the geocoding and reverse geocoding.
Geocoding is a process in which street address is converted into a coordinate (latitude, longitude). Reverse geocoding is a process in which a coordinate (latitude, longitude) is converted into the street address.
Methods of Geocoder class
- List
getFromLocation(double latitude, double longitude, int maxResults): This method returns an array of Address which specifies the surrounding latitude and longitude. - List
getFromLocationName(String location, int results, double leftLatitude, double leftLongitude, double rightLatitude, double rightLongitude): This method returns an array of Address which describes the given location such as place, an address, etc. - List
getFromLocationName(String location, int results): This method returns an array of Address which describes the given location such as place, an address, etc. - static boolean isPresent(): This method returns true if the methods getFromLocation() and getFromLocationName() are implemented.
activity_maps.xml
Add the following code in an activity_maps.xml layout file. The EditText is used to input search location and Button is used for click event to search the place.
build.gradle
Add the Google Map Service and Google Location Service dependencies in build.gradle file.
strings.xml
google_map_api.xml
Place the Google Map API key in res/values/google_map_api.xml file.
MapsActivity.kt
Add the following code in MapsActivity.kt class file.
AndroidManifest.xml
Output:
Note: Check it on the real Android device for the best result of output.