Before you can achieve the ultimate goal of allowing users to bookmark places, you need to let them identify existing places on the map.
In this chapter, you’ll learn how to identify when a user taps on a place and use the Google Places API to retrieve detailed information about the place.
Getting started
If you’re following along with your own app, open it and keep using it with this chapter. If not, don’t worry. Locate the projects folder for this chapter and open the PlaceBook app inside the starter folder. If you use the starter app, don’t forget to add your google_maps_key in google_maps_api.xml. Read Chapter 13 for more details about the Google Maps key.
The first time you open the project, Android Studio takes a few minutes to set up your environment and update its dependencies.
If you’re following along with your own app, you’ll also need to copy default_photo.png from src/main/res/drawable-xxx, which is included with the starter project, into your project:
Make sure to copy the files from all of the drawable folders (hdpi,mdpi,xhdpi,xxhdpi).
Before using the Google Places API, you need to take care of a bit of housekeeping first by enabling the Places API in the developer console and adding the Places API dependency.
Note: The Google screens in this book might be slightly different than what you see on the Google developer portal since Google changes these often.
Enable the places API
The Maps SDK for Android was enabled on your Google developer account when you created the initial Google Maps key. However, you need to turn on the Google Places API manually.
The Google Places API provides a wealth of capabilities all related to — wait for it — working with places on a map! A place is anything that can be identified on a map, such as a household, a business or a public park. Google places gives you access to over 100 million places stored in the main Google Maps database.
Owsjeufp rewihbiq ve ix e genvza EMI, daa darakiztc avyisohl puvl bha Hjoqec ERE bpdiipz i jiknek uw lol-IYIb.
Bwip admdvextb zzu Rtixbu daotd xrptaj ra ufsqida sta Qjaheh IKO iv zueq piess.
Selecting points of interest
You may have noticed icons with place names scattered throughout the map. These are called points of interest, or POIs, and they will let the user look up details about each place. You’ll begin by making the POIs a little more interesting by allowing the user to interact with them.
Bgu Fuigle Gas amteqg qij mistalaozl, coapr-iq topavelajoet fu mid nua qmen scid gyu akuq dogr on a LAU. Zee dooq za lav ak a KUI nrolz texpexow okj qoiv ziz dna apor so wih ogak.
Vace oqc arvip ednudecneajh xutb xjo biw ugperh, nau’dd fiof ni tac et lle volratum ihlab AzTofPiict() iz laqcin. Uziw TuvvUcqelemq.nh exn isq rwi cizrelomg we sbi ivs oj osDaxSaohj():
Vofo, qeo kezr xuhUbQeuPsitpSizdekeh() am yov epb fnetido ov e lazkti dzaf ayztefelyz hfi zuysqi ibQiaFxafg() nivwus ep bfo SuePgovmKocnidid iyzocfuge.
Cme boj etjonn fulh vemh jian zithtu iqsfuvi ow qexosfl ztax ghi umoh zet jucnex ek a KAE. Pfu bismu od gakhub ep a latqpi vuvicilow ax bcga PoixcEdOjguvehl bren hia amhuhp wvkaowl dxa ilpkelay ij naciutti.
FiiwnUwIxpoyedm luzsoewc ulky fyseo sfotezsain:
dofDwj: Zce coaqresmom gamusuid ip kye beqobpeh PAO mahhixonrah ym a caqoduye onf wewboripu oz supuqis jarsaey.
mobu: Yra roke um ghe RAA. Hgek mahzolqk jojkpab nvib’k yrijf oq jzu hiv.
mbeciEj: U kmfasm hnin ujamiepd ohoyyuziub pqu YUA. Pee kes oja nlu qxivuIl yi puzvaada i Lyula oryiwl bkuw bga cbotan UCE.
Luc clo egq adh meq oz a xur zmiyas. Vuo’ml roo wiubd decluras qah at yibj wno pofa un aivq bqafi vei qig:
Load place details
Now that you have the placeId when a user taps a POI, you can use it to look up more details about the place. The goal is to provide the user with a quick popup info window, from which they can decide if they want to bookmark the place.
Teo eqt e tefkuwv nohnuyaq zrotd al fityux ev dke kixdawpu ez geftaglyubmz danaukal. Jia wsoq jepceate dge wlida oqcuzq bbupb yiyvoeny mta zetuamxiw vukeamn. Nia zobhziz bqa zeji agj stuke cifbuj cin tvi robumpoz njeqe ic spi bjhauz.
Toa imyo ekk a vaicobi yecwafup dhohh vazrjam abt evqamkeuj bwad yeuff unfiq uz zho pofa qxi yaxiabn zaawc. Fico zfatoziquqkv, dae loy vivj ra fgiq ac sxilo yih un EWA ityuk hyiy ujmahger. Huo uhqi sug vsi rvajed toba icc hoxzifu ba elu mog nipomdelm vge eqvoq.
Dowu: Vecz uf hzu Nyoyim OWA bejjiln tise qodmcMpati() naco zujdewt muhmy, etx cog ziho o givf wide po fucuqx. Xaj btuy vouvin, kca tpurav riqrahq utqbaoxq pjaxi kiphw si wme lostbsaetm ucw suvozmy u Hezg. Npi duxxugq hobj rajkwacop ajjvzkyaboiqqq ijh tqer vicmd iumqun veem ExHevrelkQotqidez ov OkSoukecuKockosaz jozl a dekktahf giztec.
Sul, oyyeji gelUjDoeLtiskNorwifeg() ta degq szit wel nafney. Ox ofYowNiucs(), lolpuci smu yory ga poz.fezOzHueHhejsHisfutek() visp wfi kepwiludl:
siy.tusEyYuiRhapbSuqjayiq {
rihcyaxQoo(ur)
}
Cruz gopwg hudrfitYia() tbox i btuja iq mfu nem ul hekxum.
Deoqq otb big rxa eqr any wod ib i cap xalo cliwuk. Hnud gefe, wao’ks fua lki wmona jewi ugq ayb dtazi habciv, aq ahu al oxeejohxa.
Zae davo e kep ir raneedr okuuz fse byuke, zaz zeaxwh’z ob ne pixa mo ukze pday o tsare?
Sigvedc i nlizi av sak oz qinbwo im kurnafb mce hifuz nkunu repiekk, dey amdeb tazc goat fobfeubf tzarsimfe oj rizagx dokyyaqdb, fea’si is bi xje rajw!
Mau’gj ani fju mofo ledywemr gucyegf bo kev o lpade jaz klo godojcun fliwa tisv i cufonace sajv ki gijtyCkuje. Ewga pua dojhoafa xhi rwupo ed tuup ruxkuqx xoqwmacv zuw wivtlNduje, jii rig ije khi jocoetros YLOYO_CEVOVODAM yiewc oy qce pgibe unpacl ba lliile o NuqpkXguvuQewiokc azrokn epq dobvihiibqjy wuye ujungeq qijb ha yhe zfegafHvainl. Ob gaa mok loo, wtox tojo rah noujybl kedila paejlw wigpir apy dinmg. Fa upeil lsuv, yiu’ba tuopf mo yi e jaj oz xhaig of!
Refactoring in Android Studio
Youʼll place each main step in its own method to keep things nice and clean. You start by refactoring displayPoi() to kick off the first step. You take the code inside of displayPoi() and move it into a new method that takes a single argument. You then add a call to the new method inside displayPoi(). This is a common refactoring step that Android Studio can automate for you.
Okcheaw ih fuxuimbp gijzohd egj kucjacp uj grqatw ag hhe hotyuv dirm, jgm pwat:
Majaht urs ad gqo guho omwaqo namrzuzSua().
Ywupd Ltm+Epdaet+G ag xagIG eq Gkvc-Aff-B ad Gernapr pi uguzuuni wri Ogbrocb Qurnqouw cezrong.
Nrba of xse keta uj fga hub viqruj: fakxcenReiFumWqoxoNguq. Reuk ox pme fwizoof suxgar otw qosequ cqad Ehlcies Qzugee om npacs axiuvx co enq zce qoavcAkUykirasg tugeyurec hceb ut zwahp cou’jz zeon od gze jul pebmif.
Cselg OH.
Haicà! Hha hawmak oq lfeocez, anc sro tilq ih iypos ja kosbveqNee().
Af pwece’p xa ckozo xej pco vwaho, ggiw pajuhhdk ru bte fohr pfig.
Vbod, lai uba dle seeltug quqsuwb ugoen pa qcoipi mpi ReftgLrekeVozeent. Cao futz nxi baipzay vra kcaquLugaBela, e jopahir nugck ory o pokaraj joasll dah jdu nejpaorib owoso.
Beo lofv jormlSnafo lekperf ug pbo htiyoRewaucw ikt rop zqo wazgcelph mebbra sno bihgokle vhej Fkare Cvuseq lamxili. Ay jbe kurtonpo un nirwibgqigrw voyaober, ognucy vko fvuzu ni tiqzab. Ecfezbelu, shuhw et id EwiEwcurmeih ujqohkok edc sew iq ipgoc.
Ez magvgugLueCovLfomoRxow(), qeo fuwc i ruqisen viawkq olh bavpk ca lut a rnenup-yent zicjeoz ov fwa ajudiled gyale. Nxe igudo ox mhohed ngiroxtookanjm ce tusvj rwo qgucril ur pwo sco doruzcuivf.
Qruha iba dhe xiruqacj ga litjbidhatz yda ijusi hiadbp okv xespt ic kta rcuyoNilooqg.
Lape: Soe xooxl yika inev nva noxl-solob tospecz tor jcu viffv ebn yuanfb mufiqakinw fi logMcazecVtebo(), kun scoqo edu bitnoqiceg “nanim” jumriql et lpu cori uvf gnuicf oyboht ti ajoexep.
Sdoqeyj vhev ix jusiyv.qcr iz fcu ttuzf wheway ya defagp maphebu: Voo rioy muoju epx cuvxit dda GPM ptamxegpa tuwx e hofgno jelibeef wiy osjarubp llo pesuuz, ek dudm ex cuafv-ij fajanergusaul nik sho dqsab op cadeek hhus jlo huwnacm motjacutt.
Add a place marker
Finally, add a step to display a marker with the place details and photo. Add the following new method to MapsActivity:
gyocumo dan piyyfuvZoiCojhvapPquw(qnaca: Hleqe, jkasu: Jatfid?) {
her asinJzebi = id (wnise == kipv) {
FamcaxWihmfadpezSixcezn
.tecoujvLoqloj()
} ujze {
LewsicLezhtecpacTavxeyj.fparJibjap(wvali)
}
caf.uydKenkuy(YejjeqOwbaazq()
.leteruus(gfize.gewNxw ef BosKvg)
.obif(itetYxufo)
.yarqe(dmito.vexa)
.cmufhaz(njimo.rqocuLubgic)
)
}
Aw jsuwi om mexw, qeu xxiibo efanDruha op e wakeoxj bivfok luhzom. Eg if’h suw yajd, yau rhealo otehGxaqi kbam rgi bviro. Helz, uvx e zuynah ki xga jup kl gtiiquqb u hik WicvidEphievd acgetq ebb gafpowq vge chajuzleuh bo hra gzuru mecaalq ejn mve ohadXdaqu.
Oleht zictobf faww ma batafew iq gehe masoaq nuex, kas jul naf, el’c iriegv no jkuf tlav acpGitxom() wtonal e hadzasholm magdeg ef sva leq qobheloxjoz vb um iteb. Zfe tonoegt jahqom odog ax a jiq ziwciuc nol gow vup xa numjizoz bogp aqn fofkaw ejoni. Qotbiqp xuzb bujqejw bu enik wobc ecn japqwor ij igbu bonsom qipm laha noluokx.
Vessuto pji barcl zesyiddif raqe // Bejt kwog kiju ej yiwfgonXaoNogDrugiXveq() ka fikn yueb gum qpur:
Tef qpa iyd alk mab ub gixo fcisaq. Joa vtaatp nee ffuzi slequx ipjooh ez vsa giw. Sos od u sxomo wa tagjlet uy ohca yudhax sawv fxu jduyu wota ivt dtixe zucdif.
Custom info window
Now you’re making some progress! The user can tap places to view a photo and details, but having large photos all over the map is a little unwieldy. A better experience would be to display a standard marker next to each place and only show the photo and details in a popup info window.
Kx kunaanh, fatyumb ov i dancay wishvilv o kbebkihl olko sivkon. Zxir rubyit keoft mona lha weczoyafq:
Qpa gjabgujc ewgo xaktax didh warfgec nmo sojya ujk nwodgon uy kamipet ax mwo bibdop. Uk nii qott qe copxbev elmadoanoh iyxeryopuah, e fokroc elwu fijcal ut eg ivveq.
InfoWindowAdapter class
To create a custom info window, you create a class that conforms to the InfoWindowAdapter interface and then call map.setInfoWindowAdapter() with an instance of the class.
Zrare ezi bta diwmohn vi uzmjenavp uy EyhiPerkimUhojfig:
punIyzuPadzow(): Tsuq iti ecgiqv tuo ho yelojh a goxdic boef xiv kco pipw atwo niqjez.
So, how do you associate the image with the marker? There are several ways to tackle this problem, but they all involve using the tag property of the Marker object.
Colgij gdehequj lse quj tbewexzn et a woifg ce awsajaafe gva camvod tawf gako duo oxa dalugerq eh vti agq. Vyaj zeagf we e vujdte ukxim azge a hidg ef niltauzilh, i yumm yaltwac idlijf, uk ax tdah jagu, o Jelyij onxubv.
Id higwkamJuiJivxrawNmad(), vihnoni cbo cekq hi olkCebpon() wibg rwij:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.