Hey guys this is Priyanka. In the last tutorial we completed the get current location part and this tutorial I'm going to show you how to Search for a particular place so first we are going to make some changes in the layout file, so open The res folder you will find a layout folder inside that and click on the activity_maps.Xml file And click on the text tab so you will be able to see the code So as you can see there is only one element here. That is the fragment so we are going to add a relative layout here select relative layout and This fragment will be inside the relative layout So just open it here and close it after the fragment And it is giving an error because we need to add the two default attributes That must be added those attributes are layout height and layout width So write android:layout _height and android : layout_width And for both the values I'm going to write Match parent Match parent means it will take the maximum value, and it is still giving an error so click alt + enter So this line will get added here as you can see the layout height and width Attributes of the Fragment are also equal to match parent So I am going to change the value of height So I'm going to experiment, so I will write 400 It is too small, so maybe Okay, I think 470 works. So write 470 DP and then click on the design Tab now click on text and Drag this plain text here and These are all the properties of this text field so I'm going to change the iD so write tf Location because the user will enter the location here, and I am going to remove this so This will be blank and I'm going to change the width, so maybe 200 is too small So yeah, 300 dP.
And I'm going to add a button so click all and drag the button here and for the button also, I'm going to change the iD select B search and For the text I'm going to write search And there is one more property that I am going to change so search for on Click and Here I'm gonna write on click or you can write anything here We just need to make sure that whatever you write here. We are going to make a method in The Java file with that same name So I just wrote on click here, so we are done with this layout file So we are going to work on the maps activity dot Java file now So we will create that method now, so write public So write public void on click and here it will take view and it is giving an error so click alt + enter so This class will get imported then we need to check if the user clicked on the source button or not so right if v.GetId() is Equal to R.Id.B_search? Then Once the user clicked on the search button. We are going to take whatever the user entered in the text field and Then we will search for that place so first we need to get whatever the user entered in the text field so for that write edit text TF location equals edit text Find view by Id r. Dot Id dot tf location that is the Id of the text field and here click alt enter and click on import class So this class will get imported here Then we need to convert it to string so it's string location equals tf location Dot get text dot to string Now we need to check if the user actually entered something or not so for that, write location dot equals and I am going to add a not here that means if This is not an empty string then we can move forward So I'm going to use geo coder class here so as you can see that the Geo coder class Has these four methods And I'm going to use the get from location name method which takes two attributes Location name and Max results, and it returns a list of addresses you can find all of this information on developer.Android.Com so I am going to create an Object of Geo coder class.
So write Geocoder geocoder equals new Geo coder, and I'm going to pass this Here for the context and I'm going to create a list, but I am going to declare. It outside. The if block so right list address address list and I am going to use it here address list equals geo coder dot get from location name and It will take two attributes strength location name and Max results so for the location name I am going to write location and for the max results you can write anything I am going to write five and It is giving an error because we need to insert this in a try-Catch block so You can just write alt enter and click on surround with try/catch So that error is gone now Now what I'm going to do is or if you search for a particular location This can return a list of maybe two or three addresses Then I am going to put a marker on all of those three addresses so for that I am going to use a for loop so it for int. I equals zero I less than so now we need to check.
What is the size of that address list? So for that right address list dot size I. Plus plus It is given an error because we need to initialize this variable so you can just initialize it to null Now inside the for loop I am going to write address my address equals address list dot get and here I am going to pass I and Then for each address. I am going to create a lakh line object and I'm going to pass two values and Those two values are latitude and longitude. So write my address dot get latitude my address dot get longitude Now we need to create marker options.
We can create marker Option outside the if Block Select marker options mo equals new marker options and We can set the position inside the for loop So at mo dot position past like lankier I'm not going to add a title but you can add it like this mo dot title and then Write anything you want You can write your first result and Then you can also set an icon But I am just going to leave it as it is because it will just set it to the default icon then I am going to add this marker to the map so write M map dot add Marker and Here you need to pass the marker options so pass em Oh, and then we can either animate the camera to that location but it can give you multiple results, so when you do this the camera will focus on the last result given So you can do it or you can just leave it as it is but I'll do it She will be able to see what I am saying - em Math dot animate camera and pass camera update factory dot New Lakh Lang and pass Lakh Lankier so now we can run it and test it so connect your phone to the laptop and Make sure that you have enabled usb debugging if you don't know how to do that I will put a link in the description Box below and You can check it out so after that click on the run button and select your device and click ok So false it will show you your current location And then you can enter any place, I will write Paris and Click search so as you can see and added to Markers and Switch the view to this location because the last result was this location I. Don't know there was a place called Paris here, okay, so This is how you do it so that is for today if you have any questions Then you can email me or leave a comment. I try to answer all of your questions So thank you for watching this video Make sure to subscribe to my channel and if you like this video give it a big thumbs up. Thank you.
And I'm going to add a button so click all and drag the button here and for the button also, I'm going to change the iD select B search and For the text I'm going to write search And there is one more property that I am going to change so search for on Click and Here I'm gonna write on click or you can write anything here We just need to make sure that whatever you write here. We are going to make a method in The Java file with that same name So I just wrote on click here, so we are done with this layout file So we are going to work on the maps activity dot Java file now So we will create that method now, so write public So write public void on click and here it will take view and it is giving an error so click alt + enter so This class will get imported then we need to check if the user clicked on the source button or not so right if v.GetId() is Equal to R.Id.B_search? Then Once the user clicked on the search button. We are going to take whatever the user entered in the text field and Then we will search for that place so first we need to get whatever the user entered in the text field so for that write edit text TF location equals edit text Find view by Id r. Dot Id dot tf location that is the Id of the text field and here click alt enter and click on import class So this class will get imported here Then we need to convert it to string so it's string location equals tf location Dot get text dot to string Now we need to check if the user actually entered something or not so for that, write location dot equals and I am going to add a not here that means if This is not an empty string then we can move forward So I'm going to use geo coder class here so as you can see that the Geo coder class Has these four methods And I'm going to use the get from location name method which takes two attributes Location name and Max results, and it returns a list of addresses you can find all of this information on developer.Android.Com so I am going to create an Object of Geo coder class.
So write Geocoder geocoder equals new Geo coder, and I'm going to pass this Here for the context and I'm going to create a list, but I am going to declare. It outside. The if block so right list address address list and I am going to use it here address list equals geo coder dot get from location name and It will take two attributes strength location name and Max results so for the location name I am going to write location and for the max results you can write anything I am going to write five and It is giving an error because we need to insert this in a try-Catch block so You can just write alt enter and click on surround with try/catch So that error is gone now Now what I'm going to do is or if you search for a particular location This can return a list of maybe two or three addresses Then I am going to put a marker on all of those three addresses so for that I am going to use a for loop so it for int. I equals zero I less than so now we need to check.
What is the size of that address list? So for that right address list dot size I. Plus plus It is given an error because we need to initialize this variable so you can just initialize it to null Now inside the for loop I am going to write address my address equals address list dot get and here I am going to pass I and Then for each address. I am going to create a lakh line object and I'm going to pass two values and Those two values are latitude and longitude. So write my address dot get latitude my address dot get longitude Now we need to create marker options.
We can create marker Option outside the if Block Select marker options mo equals new marker options and We can set the position inside the for loop So at mo dot position past like lankier I'm not going to add a title but you can add it like this mo dot title and then Write anything you want You can write your first result and Then you can also set an icon But I am just going to leave it as it is because it will just set it to the default icon then I am going to add this marker to the map so write M map dot add Marker and Here you need to pass the marker options so pass em Oh, and then we can either animate the camera to that location but it can give you multiple results, so when you do this the camera will focus on the last result given So you can do it or you can just leave it as it is but I'll do it She will be able to see what I am saying - em Math dot animate camera and pass camera update factory dot New Lakh Lang and pass Lakh Lankier so now we can run it and test it so connect your phone to the laptop and Make sure that you have enabled usb debugging if you don't know how to do that I will put a link in the description Box below and You can check it out so after that click on the run button and select your device and click ok So false it will show you your current location And then you can enter any place, I will write Paris and Click search so as you can see and added to Markers and Switch the view to this location because the last result was this location I. Don't know there was a place called Paris here, okay, so This is how you do it so that is for today if you have any questions Then you can email me or leave a comment. I try to answer all of your questions So thank you for watching this video Make sure to subscribe to my channel and if you like this video give it a big thumbs up. Thank you.
Labels:
UPDATE
Thanks for reading Updated Google Maps TutorialPART 5(Android Tutorials). Please share...!
0 Comment for "Updated Google Maps TutorialPART 5(Android Tutorials)"