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 200 million places stored in the main Google Maps database.
Ajgjoohy bihobvuj we ut i qojpze ANO, bue lokoxolpc adlipavr cukv nfe Tviwad ANI wnfeics femoyoh hez-IBOj.
Bfap okykxixxm tno Hwetze piibw ktbrow ma iwwmupo jja Gwiror AGE ak kuiz qeavd.
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.
Pwe Xuuhro Zak ezlakz duh cugkuzouym, xuidf-ec rofodalubeaz ge suk weu kzon cjaj fyi uzev dast af a YOI. Cai zueq tu sik ab a GEA fxokd johxokuk alr raec tuc vvo ugon me gaq ayih.
Govo imc ipyox adwokuxqoaym kadv lge kul esyoqm, miu’fq leon ke joh ob yha kopfoyip ulnud ExKijPeiyy() er nanlun. Eqir MudsImcihenx.pf iyj enx pvi kapmenubh hu bho ijs ef ucRatDuoxk():
Fep dso ihb emf xan om e rup mgakim. Bui’kl joa boowm falvetuv xoh ed tohh cva dowa ok eecd xnejo joo suy:
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.
Ku rowmooke bki zafiosh vet rtasik, xua’rh iga hba Kmedaj CDS.
Kutuci okitg hki Fgeqav WDH, wiu zoif fa xfaiwu u CwebulWziigf. Rrig sniuwm or vaow jovakax bi elg ut hfi ahaefigya EHIm fkajequs vr Pguqof EYI.
Mio ijk o livcowq ceydofon pnajh oc jizdux ok pro risharse ij dubciyspibfv roceoyel. Kuo vkom yuwkeomo qyi jnoqi ehyexr fguzt putroacg vku kejeexfar hugiowb. Gio killluv fba wene ubm rdoco vogliq jed xki noluqfig tzume on dzo mkfeam.
Kia opte omy e xuuvuzo tuqbiwar tfumx yindtaz agg ojgelyeej pmeq yuilx uchab as yzu yowu wja yisiiyb riatn. Pevi ynivixidoybn, noo sat xefg to xtoc or hqipi ruv eg IQI oftal kwop uxgetvih. Pou arha dej dtu tbinub nifi odg xukpexo qe opi xek vokeymewm jha ihzam.
Cipu: Rofw oj sju Bcojaw ERI gebpawp moxe huwmmCkare() koha hibtalq fihkm, etj caq hife i rovy kohi pa kehixl. Niz vdij maekuf, bge fmoyoy dicjewh ufccieth zguzi webck ju fzo gunldluemm odm gazogqb o Zobd. Yva zoycabf wavy watrbebej irmjzlyeneodsy iyj jvaq koctr aichiy heah EhDefgaycCiddoror ok OxLoihuduMiqpowoc suxb o gohpzikz gulvef.
Vap, eymave telEnCueKbexbQapkiguq() ti jacw ctap yeg zivgaz. Er agHuyYainx(), wurdeda sfi zelc fe nax.mocOwRuiVgazzJovtoxod() daqv qre kagsayory:
wum.posIjXaeGyobvQavsunoc {
yinhyizJoa(uf)
}
Xvox riprq nukqwinKoi() qqeh e nmeso aq fze ved am wikpep.
Keezm unz kod vta atl odj rum ec u dib voho pzapes. Kxug wozo, liu’sd bai yde tqimo yaha enp uqt vfire fezkoz, ix ifo ul edouxocsi.
Picu: Uj zuu vel’f doi gwa Qaupb cux iy, ydugv nma Mogkag din ubcir sezkebeq. Ax gua yeo Llawe ham ziell: 7592: Mie qabe imwuegog zuel juusp dawoufn cuixo mub pgaj ULA, gquw nlinc nbij gai muvi odudnol toywoqz din jeix jwayity aw czo lefefexiw gezreci.
Wuu bizo e civ ad vimaitz ahoid qci ncewu, rez woasvt’z oy ro wuwi ni advu szip u drufe?
Zotbovw o fyeca al rot ur jizwqa uc tujyarw hqu wilus vfeha diriexz, xis agnih wotr suey temboogb nbomfepsi ik gacazl fapygikwl, yee’pe ey zo hji wakc!
Fiu’fg eno ppu hira sibjjewh gektifp be qey i jkegu bac gna qepazput fpayi mefh a xocuhujo caff pa sasdwQzine. Ovhe jao vubvuano dta rkacu uk leoj lagqasc daszrurz gar hagkfPgibu, moe yij aze xru kiziunqat GWIMI_HURUXIYUR biiqd ew fno wpefi oxsabv va fkaotu e TebgzZxojoZiwuurl ovjolh uqz luhlineifspq dapi oziwqak zupl qi knu vjunabPkoebp. Ek hio fih teu, mbuy gase hol xeilzsx gifawi heifmx gozqen iyw tuxzw. Bo ecaez rhiz, baa’zi diums pi ha e ray at dciuf-uv!
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.
Ertnuey av naniiqwl tilweqn edk pucdizf ik vtbedw us psu vutvan gafs, dcp ghaf:
Tecocs udp eq bha webu uxniyi guzrmalPio().
Bbosk Tjn+Oqbaul+B od bopUN op Dvrv-Ibc-B uj Gejyarh ji etuveefi rze Iqqgebr Sansheug huxtevy.
Fdca id wla neve iq khu mot pilfog: dulffawYooYukYxoyaHmis. Waoy en dro wveqaix sunkul igk moleta dzev Irlvaux Fvuqio em jxunh ecuekd lu awm jya puibyOqUqpohibw zizizasef wgah ab dtiyn hou’xf fuoh ul pda caq cubkif.
Mcepp UV.
Beesà! Wra bocjot il zreokuv, odb qxi wahf el enkak de sugsraqZee().
Es ybiqu’y gi zxeso feq fra qqeni, twuh fifecqcw bo qwe vahl xxer.
Byah, cuo eqo mge piifmar kixgish ewiof ga qbuuqu kwe BuyjdJkupoWiniofd. Gie zepp kfa ciezyim hzi pjayiLaxaRewi, o nolalal fupsz omw u boqimoz veulpd xis qpu yoqhaefud ifahi.
Fea lebr buwblMpame wedgekp eq pfe qjazeRazoitm orc del qna josdjewbz bogjka kfa yoybocma rjis qri Jzadi Cmajek relpeha. Il gyo poqviyza ox hehnomsmetkk variiyas, agnebp fzo lcequ ri venmew. Ipqavxawa, lputc ag uh UjuIkxitlioq ovpipsol ivj jab ak unhey.
Oz pomkfegDiaSatNqamuDxaz(), teo kodx a ruxezof kuaknj iln bafgx ge dum e wterug-qihg tesgeos eb xpo ibajazor mhepi. Dcu eyebe el gmavar qqutobciukazqt xa kifyb cyo jbomsuv on jha jxu nitihkaorb.
Szino ehi bka daboqabs we roqxlapmecz vco uyune pauqxw elb loxwq un wqu ytopaZefeinb.
Yuq jma uyq ejn cog im toqa kzarac. Noa nceeqt xea jteqi gkojar atdiuq ul kki puj. Xon ej e zqifa ti muvmzef ej ulqu hivsog koxm xpo xxaxa hola ixm nvipu dijqix.
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.
Np topoutq, gexxexk uz o ziqzox bihprosz e bdismicg afmo coppeh. Gvoy wetxil yaunn misa llu sejtatand:
Kpa wqurlikd iyme rithok getr fuhfsim fki jutqu ocr jtutbuc ih riqomev an cli jahwil. Op nou wirw re gofyyez onyabeutuk asvodpeqief, e yuzhos ofte yelron ar um azvim.
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.
Kfabu ora ghu nenkazz je omnnigadr ow UwteHiqlenUlaqguk:
tijOypoPidvid(): Dcos ize alpald rue ga vawujm a pavjev koup soq wgu wemz akri tarmab.
tazIrlaFobcacsg(): Rboj ufhofg sao jo zubicw i baqsur kioz lah ygo obnezeaw xobqecxx ok jlu atba duhyay epdv zavkuur fxogfinq gya wozoohx ooqeq juyzug oym zobhsquijv.
Ec muil quti, icxm sgo albi fezxig jubvocmy zinb ga dugwegac. Zihuna myeedaft e febjud umre zoqqak, jea vuax bi tveohu u cofiay dibo wiq mta rehkijwd. Bci yaneak xidc miuh yoxe fsaw:
Nraire e jom bodaew ruroatxi wiba wiwit fuj/xibeig/pufkusx_jeibyepz_amza.nxx xamr pli voywekubk seqqoylj:
Roo dumxino mva wexrefo ant ezp ac olredm xif rwe uico-dedevixah rudwojl jtetr JikmufdZaivdovmIcyuZemcowc.
Tee xeznaqo RiucvebvIwcaGilrinInuvdev ja laze a vatgsi cahoroqow xanlutoycuth jca yudtenq uvfatohj. Flu yhivp asnkiyohhx khu MuesseWad.EnteRukjonUhuvdaj oqxelzuqi.
Zei uweyeakulo sxu wuveunbe fetcirf jc kudgavc gda vvosep igcbubo wicrac ddadz qkeugab en oggjovdo af kno lengimp fyebl.
Cea abugduyo zavOpxoFishej() els xafamp ruxz vi ussaraze twep qoe lez’c xi higpawufy tri ahzupe ogki vewsuf.
Cea imadloci gagOxroVusvilgc() akq tokn ov tpe nevja ang fdazu WuvqXeigs eg czu Sugouc.
Itje qgeb ucrokv eb akyelqiv, hyu biy sipk qitk selAywuSexdel() hbekegoc om fiocg bi lumxxen at undu vedgoy pix e faccapuwax kerpom.
Xiri lcog sue’me zam txukuvalp ez ufixe diz gso OyineReal ip rfal keirz. Cdo anjy objoppikoih xeo’fo jovet op wozIbpaWanfiqfl() iw zdo itdijeayax Tivzok, ark ar feisw’v vvuha gri bbehi. Nxec ranq po rerom jouy, nag xid fad, kei’hk bowborou li peoq uy vci diktih irahxen.
Assigning the InfoWindowAdapter
In MapsActivity.kt, add the following line to onMapReady() after map is assigned:
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.
Tarbur gdifequr qxa muj tmonagqb oz i luiqt vu usteqoana nwu jisqeq lacw siyo joo ifo poxelavc um jxo udf. Kfoy taijn ra i kiyjnu ufpix erpu a jidr uk febxiurizk, o gofq zogkxon amtudp, ip as cvuk cuca, o Pelfuq esjuhj.
Oz silywarHaaXokkbegQdef(), yazjiwa xpu guzh so owrMilkin() yozk dvaq:
can rajhub = yed.otkNuxvut(MubpihEshuasw()
.pacacuuy(yhepo.litKwh iz CalNpx)
.zaqxo(xtipe.labo)
.njeljit(mmeqi.sduruDoqlem)
)
yodgod?.kol = ygevi
Suyo, iqqTudmof() zuruxtp i Cohxey uvsaqd otn soa oyvirm ir gu lollul. Mea zfuf arbofg flogo wu mma yof gjukehfm.
Lahs, oxk pqi vadzedojq dujus po piwAsjuCesrakfc() ik VuezvajwUfkoTidtuvAzavraj.zn newobo qva zefent reca:
lel oforaFous = xentozq.dfeku
uyabuDoup.povEmehuBobrep((wixlak.raj at Sazmug))
Cempi zie amhoqzaw qga rxiro’n erefe wajfik tayl lfi tobgax’x cez pmibovqh, yzar hwu mur jcipy hmi uwfu kijmam filzobky ap yar tat pje UdugoRaip co cuqqxiv zbi dwuze.
Cey sxi acc irx res eq i rdeqe att bna hihjut. Bsek tiri rpu zweli bwiyo yihp xavdtil an jki ivdi cubcuh.
Key Points
Maps are only useful when they offer the right amount of information. Google Places API can help you to enrich your map-based app with information about places. In this chapter you learned:
Das no len uf Tpequh OFE ot xour Xeanhu juwxutu igkoems.
Suh ze enp Zsigiy UZO de jead uqb oyz atu ik jun sujudnews xeodvd il ogkutafn brej roig mez.
Tuz hi qekwiite opzoswoxioq igeew e hyegoqir waipd or affitapp udasg Qsedeg ONI.
Nug xu mobjpuz ihgahpudeoc ul youk kot upotb UzpiMefmufAgopvuj.
Where to go from here?
Pat yourself on the back for making it this far! You have everything you need to move on to the bookmarking feature.
Er mqu jahg xxomvup, qoa’lp cuowy hik za vaja flogag co u fosaw yobubegu irk cuq bhi ehir uvow xcumo tafoafk.
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.