Android queue



hello guys, this is mai thanh hiep. today, i gonna show you step by step how to use basic google map api on android programming after completely watching this video, you can create your own an application such as finding a path from 2 any places on map like below demo application the first time, this will show place of university of science, “dai hoc khoa hoc tu nhien”


Android queue, you can also show other places, it's up to you you have to fill out the original address and destination for an example, enter original address “dai hoc khoa hoc tu nhien”, destination “ben thanh” and click find path button


wow, app will show a path from “trung tam tin hoc dai hoc khoa hoc tu nhien” to “ben thanh, district 1, ho chi minh, viet nam” and some others information such as the distance between two places is 2.3 km and appoximate time moving is 10 minutes. wow, it sounds interesting, aren't you? now, we will together dive into google map api first of all, we create a new project in android studio name your project for an example googlemapapidemo next choose google maps activity finish


after creating new project with google maps activity, android studio will show the screen of google_map_api.xml and you need google maps api key for using this google map service. to register a key, you go to google api console if your account hasn’t have any projects yet, please create the new one. click “create a project” button, fill your project name in this field and click “create” button. after done at creating a project, you need to go to google maps android api to enable this api for this project -> click “enable” button after enabling, you can go to credentials to create a key for your application click “create credentials” button -> api key -> server key -> name the key -> create -> ok copy this key and paste to “your_key_here” place, that’s it, so easy!


ok, we will see overview the source code that android studio generated we have a class named supportmapfragment and this code line will retrieve the map view in ui that you need to interact with programmatically .getmapasync(this) function will start map service. after succesfully starting map service, this onmapready will be called this code line will create a sydney position with latitude is -34 and longitude is 151 .addmarker will add a marker at sydney position to the map with title is “marker is sydney” .movecamera will move the center of screen to sydney position now, we will run and see the result ok, double click to zoom it, ok that’s sydney position


now i will change it to my university “dai hoc khoa hoc tu nhien” go to “maps.google.com” to get this place coordinate right click this place and click “what’s here?” to zoom sydney in level 18, use .newlatlngzoom(sydney, 18) function run it this is “khoa hoc tu nhien” with a marker title “khoa hoc tu nhien tp hcm” ok, to set different types in google map, we use setmaptype function google supports 3 types of map: map_type_normal, map_type_satellite, map_type_terrain now, i will change to map_type_satellite and run it


ok, this is satellite map now to allow user show their current location, we use setmylocationenabled(true), alt+enter -> run click my location icon and see the result screen moved to my current location with a blue point aimed my location to add a marker with our custom icon, add a function .icon(…) to markeroptions() like this umh, copy an icon to drawable folder and we use it as a custom icon ok, run it to draw polylines to map through given coordinates, use .addpolyline(…) like this add a list of latlng coordinates to add(…) function, that this polyline will go through


create another latlng coordinate named dhsg add more 2 mid points set width is 10, and color is red ok, now i will teach you how to find a path between 2 address places using google maps direction api you can search this api from google the output maybe json or xml, google recommend you to use json format instead of xml and some parameters, but the most important ones are origin, destination and key origin can be latlng coordinate or address text or place_id destination is end place that the same format to origin


key is your google api key that you created before but you need to enable google maps direction api of your project from google console choose your project search direction api and choose google maps direction api and enable it ok, just copy your project key for an example, i query data from “dai hoc khoa hoc tu nhien” to “ben thanh” and get the json result there are so many results, but you can focus on routes result, this is an array json objects end_address: “ben thanh, quan 1, ho chi minh, viet nam” this is the end place that google detected from your input “ben thanh” end_location is a pair latitude and longitude of the end place


start_address: “trung tam tin hoc truong ….” this is the start place that google detected from your input “dai hoc khoa hoc tu nhien” this one mean the distance is “2.3 km” and aproximate is “10 mins” from origin place to the destination you can read on google maps direction api document for more details overview_polyline: this is coordinates of the route from origin to destination that is encoded, you need to decode it ok, now i will teach you how to create simple android application that find path between 2 places by explaining my code and i will share this code to github, and you can find the download link from this youtube description. we need 2 edit text controls, the first one for origin place and the second for the destination a button find path to do the action and a google map view


when user click “find path” button, it will invoke sendrequest function get text of origin, destination from 2 edittext controls and check if they are empty if the validation is ok, it will delegate to directionfinder object to executing finding a path in execute function, it will download raw json data from link first we need a createurl() function to create a link from origin, destination and key from user input. urlencode will encode user input to url format after having got the link, we need an independence thread from ui thread to download raw data, because it takes a long time to download raw data new url(link).openconnection().getinputstream and get the result string data after completely downloading data, onpostexecute will be processed


and parsejson is invoked to get necessary information jsonobject jsondata = new jsonobject(data) will create a jsonobject from a data string. and we need routes information it is jsonarray because it starts with ‘[‘ character so we call jsondata.getjsonarray(“routes”) to get routes jsonarray from jsondata generally, this route is only one entry, but it maybe more than one if you pass alternatives=true foreach array objects, get each json object and process it like this we need decodepolyline function to decode overview_polylines of a current route and it returns a list of latlng coordinates of the route from the origin and destination


you can code this function by your self or search it from google after completely json processing, it will return list routes, and listener invoke interface function “ondirectionfindersuccess” to display the result to map because main activity is implemented this interface move screen and zoom at startlocation route is generally only one entry zoom at startlocation in level 16 we will set text of distance and duration to show it and we will create a marker at startlocation and a marker at endlocation with its corresponding title we will create a polylineoptions and then add list of latlng coordinates to it


after that, we will add polylineoptions to mmap we also need polylinepath to store polylines that mmap.addpolyline return, for us to remove it later in case we don’t need it anymore remove them if we don't need these polylines anymore or else these polyline nerver disappear in map view ok, see demo again done! now you can create your own application like my demo app using google maps api addition, you can search other common google api such as “place autocomplete” this will predict user input type and auto complete it


Android queue

or google maps geocoding api to convert address text to a latlng coordinate


it will return latlng coordinate from an address text and other useful information ok, thank you guys for watching this video please comment if you have any problems and click subscribe below to get new video tutorials from me!


Android queue Rating: 4.5 Diposkan Oleh: PaduWaras