Google Maps Tutorial : Part 2 (Current location & Search Address)

Published by Darron Toy on

hey guys in the last tutorial we created a map which is showing a fixed location near Nigeria so in this tutorial I'm going to continue from where we left off first first open Maps activity dot Java file and here I'm going to remove these comments because we don't need it and it is taking too much space after that here is the marker that is setting the location after that I am going to add just one line like M map that is the object of google map dot set my location enabled and it takes a boolean value so for that right true and I'm going to run it so I have already started my AVD this is how the app looks before adding this it is just showing a fixed location you can't do anything so now I'm going to run this application so now the app is launched on the AVD so i'm going to take the APK file like i told you in the last tutorial if you don't know how to do it then just open your Android studio and here is the path where your app will be stored and this inside that you'll find one folder named app then build outputs apk and there will be a file named app – the book dot apk just copy this file on your phone or any Android device and run it I'm using AirDroid to transfer this file on my phone so I am NOT going I'm going to show you at each and every step what we are doing you don't have to do everything you just copy all the code and then let afterwards now I'm going to run it on my phone so this is how it looks you get one button that is the location button and you have to go to your settings and turn on the location and this app will use your GPS to track where your device is situated and when you click on that locate a location button will get your location so by just adding this one line you can see your location current location now after that what we are going to do is we are going to add a search bar and a button if you want to search for a location search for a particular address on your Android device then you can do it so I'm going to open activity maps dot XML file inside the layout folder and so go to text because here only a fragment is that there is no linear or relative layout so I am going to add a linear layout which is horizontal you can directly add it so what I'm going to do is right here linear layout and you need to add android : layout width wrap content and layout height also wrap content and close a linear load here I just remove this from here because we are closing it here now we have a horizontal vertical layout and said that there is a fragment that is the map fragment and we are going to add a text field so just get one text field here and increase the length so we need this layout to be a vertical so here add Android orientation and write vertical or you can also do that here what you can do is select linear layout and go to orientation here and select vertical so after that just increase the fragments height and width like this now after the search bar we also need one button so first I'm gonna assign an ID so right there text field location or I'll write address and I am going to add one button here you can leave it as it is or what I can do is to make it look a little bit better add one more linear layout so that and this linear layout will be horizontal so there is one linear layout inside that this search well is there and fragment is there and the first part will be divided into will contain one more linear layout that is horizontal so just add a linear layout width and height wrap content and just remove it from here and added here we want the edit text first and then the button so I am going to remove it from you and after edit text I am going to add it here edit text this text field so to look something like this and now I am going to double click here and for the text I'm going to write search and for the ID right be searched that is the search button and I am going to set on click property so select the button and in the properties window search on click and right all right on search and accordingly we are going to add one method here on search so public void on search is going to take an object of view class if it gives any error then just hit alt enter so that the view class will be imported here or you can add it manually by writing input Android dot view dot view after that what we are going to do is we need to fetch the text value entered by the user so write edit text location TF is equal to edit text find view by ID r dot ID dot TF address then I am going to convert it to strength so write string location is equal to location TF dot get text dot to strength now we have the location in string form now be able to check if user has entered anything or not if location is equal to is not equal to null or location is not equals and your ad not so this means that look if location is not equal to null or location is not equal to empty string then we'll proceed otherwise it will give an error now what I am going to use is the geo coder class so you can find more information on I will add this link in the description box below you can check it out so what geo code is this class is for handling geocoding and reverse geocoding and geocoding is the process of transforming a street address or other description of a location into latitude and longitude coordinates and your reverse geocoding reverse geocoding is the exactly opposite process so it takes latitude and longitude coordinate and it generates a partial address so these are some of the methods are get from location get from location name get from location name which only takes two parameters and is present so we are going to use some of these methods now I am going to create an object of geo coder class right geo coder geo coder equals new geo coder and the constructor takes the context so you can write this now we have the address that is a location name and we need to generate the latitude and longitude coordinates from the name so we are going to use the method like geo coder dot get from location name which takes only two parameters the first parameter is the name that is location here the string variable and the second parameter is maximum addresses so I am just going to use one here because we need to generate only one parameter and it is given an error we need to add except we need to add exception are surrounded with try-catch I am going to surrounded with try-catch after that we need to store this or the result of this method so it generates list of addresses so I list address and the addresses address lists so I created a variable named address list which stores a list of addresses but we need only one because it will only generate one value because here it is one so after that after the catch right address address equals address list so we declare it inside the try so I am going to just write it here so I define this variable outside the try and yeah I'm going to remove it so address address address let's not get at 0 so click fetch the first value so this giving an error address list might not have been initialized I'm going to write null here after that we have the address so this address class stores the latitude and longitude so we are going to create an object of Lac line that is la the LNG class equals new flag LNG and the forces double latitude and the second is double longitude so right address dot get a latitude comma address dot get longitude semicolon so now we have the latitude and longitude now we can add the mark so what you can do is you can just copy the statement from your a narrative so it is setting the position to zero comma zero so just remove this much and write the variable name – la T LNG now we have the location we have added a marker on that location but we need to zoom our camera or we need to focus our camera on to that location select M map that is the object of google map class dot animate camera and it takes camera update objects right camera update factory dot new Lac line and here add the variable name that is le T LNG for me if it is different for you then unique are two you can add it add your name here so now it is done I'm going to run it again and I'm going to show you how it looks so now I am going to search for New York and click on search button and as you can see it is so there is a marker added on the location and the camera is also focusing on the location now I'm going to search for Eiffel Tower so it is doing the job perfectly so this is it for today in the next tutorial I am going to teach you how to zoom in and zoom out and we'll also see how to change the map type this map type is normal you can set it to hybrid or satellite so thanks for watching this video make sure you subscribe to my channel and if you liked this video give it a big thumbs up bye


idrissi islamic · May 15, 2019 at 3:59 pm

thank you for the video , i have a question please ,how can i search by title who you add it in the marker ?

rashmi rashu · May 15, 2019 at 3:59 pm

How to resolve getMap() error. it is showing cannot resolve getMap method

SHIVAM AGGARWAL · May 15, 2019 at 3:59 pm

After getting blue dot , can anyone tell me how to get LatLng coordinates of that location.

Luzi Aeht · May 15, 2019 at 3:59 pm

thank you

sonic blade · May 15, 2019 at 3:59 pm

Hmm, I want to ask something. How that button search can work ? I found some error that called "java.lang.IllegalStateException: Could not find a method Onclick(View)", help me please.. thankyou

Abdullah Sabir · May 15, 2019 at 3:59 pm

how to draw route between current location and pre existing marker by tapping on the marker

Bat-Ireedui Enkhzul · May 15, 2019 at 3:59 pm

How to fix getMap(); error??

scott smith · May 15, 2019 at 3:59 pm

The search bar and the button don't appear when i load that app on my phone, why is this?

Charles Iznaur · May 15, 2019 at 3:59 pm

Happy Birthday Arohi!

punjabi Song · May 15, 2019 at 3:59 pm

live map please

ahmed · May 15, 2019 at 3:59 pm

Good work but how can I make text field displays Addresses depends on first letters of address so that's make it easy

HINDU REDDY · May 15, 2019 at 3:59 pm

Can you add videos on retrofit

HINDU REDDY · May 15, 2019 at 3:59 pm

Thank you madam your videos are very clear to understand and we created successfully a google maps

Mohit Chauhan · May 15, 2019 at 3:59 pm

I use this code in my project it's working properly in lollypop but isn't working in marshmallow and also not in nought plz help me @Tech Academy

A B · May 15, 2019 at 3:59 pm

Ma'am can you provide me a complete source code or link for current and destination location by showing a line for starting and ending point on google map, thanks in advance.

Angel Delgado · May 15, 2019 at 3:59 pm

Does it work in the emulator does it have to be in a cell phone?

surabhi lakhotiya · May 15, 2019 at 3:59 pm

mam,Can you please upload a video of store locator in android (java).Add Stores from application and search them.Is it possible mam?

Harikrishna Alingal · May 15, 2019 at 3:59 pm

great, you have got 5.0.1 🍭 update on your phone

Sanjay Damor · May 15, 2019 at 3:59 pm

how to add pop-up in map ..

Muhammad Ameem Khan · May 15, 2019 at 3:59 pm

I got a problem… Are you still there to help?

Nguyễn Tuấn Kiệt · May 15, 2019 at 3:59 pm

Thanks you !!!

Hojo Ujinobu · May 15, 2019 at 3:59 pm

I have a button in my app and an EditText field where people can insert a postal code. When I hit the button, the map does not updates to the location of the postalcode address. Is this because it's a postal code but I rather need to insert stuff like "White House" or something?

jotti lohano · May 15, 2019 at 3:59 pm

When I run this code my app has unfortunately is not runing plzz help me how I solve problem

gundeep singh · May 15, 2019 at 3:59 pm

Can u plzz tell me how will I set marker click listener??

Ripane · May 15, 2019 at 3:59 pm

Hey, is this tutorial also searching for full adresses? Like entering street and number???

Ravina Bhayani · May 15, 2019 at 3:59 pm

i have error on this line….
Address address = addresslist.get(0);

Gautam Faldessai · May 15, 2019 at 3:59 pm

App crashes if search button is presses without providing input …need help.

Ariq Ahmad · May 15, 2019 at 3:59 pm

Instead of generating apk file one can directly connect their Android device by USB debugging.

sadhna patidar · May 15, 2019 at 3:59 pm

plz upload the video that finds the distance between two points on Google map

Saad Idrees · May 15, 2019 at 3:59 pm

Can anyone help me to find directions by entering origin and destination. Also which gives distance and duration, shortest and alternate routes, traffic status.
@Tech Academy Please help.


Kx Uevoli · May 15, 2019 at 3:59 pm


shivani shivanisharma · May 15, 2019 at 3:59 pm

when i click to location button it is running properly but when i search in editext ndclick on seaarch button then app is close( i think my onclick function is not workng plz help)

Saif Lesnar · May 15, 2019 at 3:59 pm

Thanks & Happy birthday arohi! 😀

maniwal rohit · May 15, 2019 at 3:59 pm

how google navigation turn by turn command like "turn left turn right" works and how i use it for my purpose?

reshmika adgal · May 15, 2019 at 3:59 pm

Thanks for the tutorial . But it would be better if you could upload the latest version of tutorial as some of the methods are depracated

vipul shukla · May 15, 2019 at 3:59 pm

Good starting point for someone starting to read Google Maps API on Android.

Doron David · May 15, 2019 at 3:59 pm

Thanks dude! Helpful vid!

azad bingi · May 15, 2019 at 3:59 pm

ur great this is awesome and i love it

Jaydeep Shrivastava · May 15, 2019 at 3:59 pm

Thank you So Much For Such As Best Explaination. Keep it Up. Very Nice

iProComboツ · May 15, 2019 at 3:59 pm

Hi! I'm trying to follow your tutorials but most of the code is now deprecated. May you please give me an alternative to mMap.setMyLocationEnabled(true); ? As I am getting this error: call requires permissions that may be rejected by the user

zain ulabdeen · May 15, 2019 at 3:59 pm


MOHANKRISHNA VEDICHERLA · May 15, 2019 at 3:59 pm

I found an error "cannot find symbol method getMap()",how to resolve this?

Srinivasan Namachivayan · May 15, 2019 at 3:59 pm

Hi Tech Academy.

Can you upload your latest code to get the current location of the user

Hardeep Happy · May 15, 2019 at 3:59 pm

hy its not working for android studio v 2.2.3 it constantly given an error

amallal t l · May 15, 2019 at 3:59 pm

Your technology is under extinction,I can't get the current location button in my screen! But your tutorials are helpful. try to keep it updated.

Saliha Arif · May 15, 2019 at 3:59 pm

best (y)

Leave a Reply

Your email address will not be published. Required fields are marked *