Sie sind auf Seite 1von 26

Android working with Google Maps V2

If you have developed any app that contains Google Maps v1, Its time to upgrade it to Google Maps V2 as
google maps version 1 deprecated officially on December 3rd, 212 and it !ont !or" anymore# $his article
aims to give "no!ledge about ho! to implements ne!er Google Maps into your applications# If you have
already !or"ed !ith V1, implementing V2 is very easy# %efer Google Maps Docs for any topic that is not
covered in this tutorial#
&efore starting a ne! pro'ect, !e need to go through some pre re(uired steps# $hese steps involves
importing re(uired library, generating )*+1 fingerprint and configuring maps in google console#
1. Downloading Google Play Services
Google made ne! Maps V2 +,I as a part of Googl e Pl ay Servi ces )D-# )o before !e start
developing maps !e need to do!nload google play services from )D- manger# .ou can open )D-
manager either from /clipse or from android sd" folder#
0pen Eclipse Windows Android SD Manager and chec" !hether you have already do!nloaded
Google ,lay )ervices or not under E!tras section# If not select play services and install the pac"age#
2. "#porting Google Play Services into Eclipse
+fter do!nloading play services !e need to import it to /clipse !hich !ill be used as a library for our maps
pro'ect#
1# In /clipse goto $ile "#port Android E!isting Android %ode "nto Workspace
2# 1lic" on &ro!se and select Google ,lay )ervices pro'ect from your android sd" folder# .ou can locate
play services library pro'ect from
android&sdk&windows'e!tras'google'google(play(services'li)pro*ect'google&play&services(li)
+# Importantly !hile importing chec" %opy pro*ects into workspace option as sho!n in the belo! image#
+. Getting the Google Maps AP" key
1# )ame as in maps v1 !e need to generate )*+21 fingerprint using 'ava keytool # 0pen your terminal and
e3ecute the follo!ing command to generate )*+21 fingerprint#
,n Windows
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storeass android -keyass
android
,n -in.! or Mac ,S
keytool -list -v -keystore !".android"debug.keystore -alias androiddebugkey -storeass android -keyass android
In the output you can see )*+ 1 finger print#
2# 4o! open Google +,I 1onsole
+# )elect Services on left side and turn on Google Maps Android AP" v2
/# 4o! select AP" Access on left side and on the right side clic" on %reate new Android key0
1# It !ill popup a !indo! as"ing the )*+1 and pac"age name# /nter your S2A 1 and your androi d
pro* ect package na#e separated by semicolon 3 and clic" on create#
I have given li"e belo!
#E$%&$E'$(($&)$*#$E+$'*$%,$)F$*F$#*$)+$+,$E-$.F$+($.%$F#$/-0in1o.android2ive.google3asv,
+nd note do!n the +,I "ey !hich re(uired later in our pro'ect#
/. %reating new Pro*ect
+fter completing re(uired configuration, Its time to start our pro'ect#
1# In /clipse create a ne! pro'ect by going to $ile 4ew Android Application Pro*ect and fill re(uired
details# I "ept my pro'ect name as Googl e Maps V2 and pac"age name as i n5o. androi dhi ve. i n5o
2# 4o! !e need to use Google ,lay )ervices pro'ect as a library to use pro'ect# )o right click on pro'ect
and select properties# In the properties !indo! on left side select Android# 0n the right you can see
aAdd button under library section# 1lic" it and select google play services pro'ect !hich !e imported
previously#
+# +dd the Map -ey in the manifest file# 0pen Androi dMani 5est. !#l file and add the follo!ing code
before tag# %eplace the androi d6 val .e !ith your map "ey !hich you got from google console#
45-- 6oolge 7as -PI 8ey --9
43eta-data
android$na3e:";o3.google.android.3as.v,.-PI<8E="
android$value:"-I>aSy#?7lkOv(s@-7ABO)/CksdaD(EE@.FLgo" "9
/# Google maps needs follo!ing permissions and features#
A%%ESS(4E7W,8(S7A7E 5 $o chec" net!or" state !hether data can be do!nloaded or not
" 47E84E7 5 $o chec" internet connection status
W8" 7E(E97E84A-(S7,8AGE 5 $o !rite to e3ternal storage as google maps store map data in
e3ternal storage
A%%ESS(%,A8SE(-,%A7" ,4 5 $o determine users location using 6i7i and mobile cell data
A%%ESS($" 4E(-,%A7" ,4 5 $o determine users location using G,)
,penG- ES V2 5 %e(uired for Google Maps V2
7inally my Androi dMani 5est. !#l file loo"s li"e this 8%eplace the pac"age name !ith your pro'ect
pac"age9
AndroidManifest.xml
4GD3l version:"'.%" en;oding:"ut1-+"G9
43ani1est D3lns$android:"2tt$""s;2e3as.android.;o3"ak"res"android"
a;kage:"in1o.android2ive.google3asv,"
android$versionHode:"'"
android$versionIa3e:"'.%" 9

4er3ission
android$na3e:"in1o.android2ive.google3asv,.er3ission.7-PS<REHEIFE"
android$rote;tionLevel:"signature" "9

4uses-er3ission android$na3e:"in1o.android2ive.google3asv,.er3ission.7-PS<REHEIFE" "9

4uses-sdk
android$3inSdkFersion:"',"
android$targetSdkFersion:"'*" "9

4uses-er3ission android$na3e:"android.er3ission.-HHESS<IEEJOR8<SE-EE" "9
4uses-er3ission android$na3e:"android.er3ission.IIEERIEE" "9
4uses-er3ission android$na3e:";o3.google.android.roviders.gs1.er3ission.RE-.<6SERFIHES" "9
4uses-er3ission android$na3e:"android.er3ission.JRIEE<EKEERI-L<SEOR-6E" "9

45-- ReLuired to s2oC ;urrent lo;ation --9
4uses-er3ission android$na3e:"android.er3ission.-HHESS<HO-RSE<LOH-EIOI" "9
4uses-er3ission android$na3e:"android.er3ission.-HHESS<FIIE<LOH-EIOI" "9

45-- ReLuired Oen6L ES ,.%. 1or 7as F, --9
4uses-1eature
android$glEsFersion:"%D%%%,%%%%"
android$reLuired:"true" "9

4ali;ation
android$alloC#a;ku:"true"
android$i;on:"MdraCable"i;<laun;2er"
android$label:"Mstring"a<na3e"9
4a;tivity
android$na3e:"in1o.android2ive.google3asv,.7ain-;tivity"
android$label:"Mstring"a<na3e"
android$t2e3e:"Mstyle"-#aseE2e3e"9
4intent-1ilter9
4a;tion android$na3e:"android.intent.a;tion.7-II" "9

4;ategory android$na3e:"android.intent.;ategory.L-UIHNER" "9
4"intent-1ilter9
4"a;tivity9

45-- 6oolge -PI 8ey --9
43eta-data
android$na3e:";o3.google.android.3as.v,.-PI<8E="
android$value:"-I>aSy#?7lkOv(s@-7ABO)/CksdaD(EE@.FLgo" "9
4"ali;ation9

4"3ani1est9
1# 4e! google maps are implemented using Map$rag#ents !hich is a sub class of $rag#entsclass#
0pen your main activity layout file acti vi t y(#ai n. !#l file and add follo!ing code# I
used8el ati ve-ayo.t as a parent element# .ou can remove it and use Map7ragment directly#
activity_main.xml
4GD3l version:"'.%" en;oding:"ut1-+"G9
4RelativeLayout D3lns$android:"2tt$""s;2e3as.android.;o3"ak"res"android"
android$layout<Cidt2:"1ill<arent"
android$layout<2eig2t:"1ill<arent" 9

41rag3ent
android$id:"MOid"3a"
android$na3e:";o3.google.android.g3s.3as.7aFrag3ent"
android$layout<Cidt2:"3at;2<arent"
android$layout<2eig2t:"3at;2<arent""9

4"RelativeLayout9
:# +dd the follo!ing code in your Main +ctivity 'ava 8Mai nAct i vi t y.* ava9 class#
MainActivity.java
ubli; ;lass 7ain-;tivity eDtends -;tivity P

"" 6oogle 7a
rivate 6oogle7a google7a0

MOverride
rote;ted void onHreateQ#undle savedInstan;eStateR P
suer.onHreateQsavedInstan;eStateR0
setHontentFieCQR.layout.a;tivity<3ainR0

try P
"" Loading 3a
initili>e7aQR0

S ;at;2 QED;etion eR P
e.rintSta;kEra;eQR0
S

S

"TT
T 1un;tion to load 3a. I1 3a is not ;reated it Cill ;reate it 1or you
T T"
rivate void initili>e7aQR P
i1 Qgoogle7a :: nullR P
google7a : QQ7aFrag3entR getFrag3ent7anagerQR.1indFrag3ent#yIdQ
R.id.3aRR.get7aQR0

"" ;2e;k i1 3a is ;reated su;;ess1ully or not
i1 Qgoogle7a :: nullR P
Eoast.3akeEeDtQget-li;ationHonteDtQRU
"Sorry5 unable to ;reate 3as"U Eoast.LEI6EN<SNORER
.s2oCQR0
S
S
S

MOverride
rote;ted void onResu3eQR P
suer.onResu3eQR0
initili>e7aQR0
S

S
%un your pro'ect and congratulations if you see a map displaying on your device#
Placing a Marker
.ou can place a mar"er on the map by using follo!ing code#
"" latitude and longitude
double latitude : 0
double longitude : 0

"" ;reate 3arker
7arkerOtions 3arker : neC 7arkerOtionsQR.ositionQneC LatLngQlatitudeU longitudeRR.titleQ"Nello 7as "R0

"" adding 3arker
google7a.add7arkerQ3arkerR0
%hanging Marker %olor
&y default map mar"er color !ill be %/D# Google maps provides some set of predefined colored icons for
the mar"er#
"" ROSE ;olor i;on
3arker.i;onQ#it3a.es;ritorFa;tory.de1ault7arkerQ#it3a.es;ritorFa;tory.NUE<ROSERR0

"" 6REEI ;olor i;on
3arker.i;onQ#it3a.es;ritorFa;tory.de1ault7arkerQ#it3a.es;ritorFa;tory.NUE<6REEIRR0
%.sto# Marker "con
+part from maps native mar"er icons, you can use o!n image to sho! as a mar"er# .ou can load the icon
from any "ind of supported sources#
5ro#Asset; Stri ng asset4a#e< 5 :oading from assets folder
5ro#=i t#ap ;=i t#ap i #age< 5 :oading bitmap image
5ro#$i l e ;Stri ng path< 5 :oading from file
5ro#8eso.rce ;i nt reso.rce" d< 5 :oading from dra!able resource
&elo! I loaded a custom mar"er icon from drawa)le folder
"" latitude and longitude
double latitude : '*.&+A%((0
double longitude : *+.(+//*'0

"" ;reate 3arker
7arkerOtions 3arker : neC 7arkerOtionsQR.ositionQneC LatLngQlatitudeU longitudeRR.titleQ"Nello 7as"R0

"" H2anging 3arker i;on
3arker.i;onQ#it3a.es;ritorFa;tory.1ro3Resour;eQR.draCable.3y<3arker<i;onRRR0

"" adding 3arker
google7a.add7arkerQ3arkerR0
Moving %a#era to a -ocation with ani#ation
.ou may !ant to move camera to a particular position# Google maps provides set of functions to achieve
this#
Ha3eraPosition ;a3eraPosition : neC Ha3eraPosition.#uilderQR.targetQ
neC LatLngQ'*.&+A%((U *+.(+//*'RR.>oo3Q',R.buildQR0

google7a.ani3ateHa3eraQHa3eraUdateFa;tory.neCHa3eraPositionQ;a3eraPositionRR0
7ollo!ing are enhancements and features that google maps provides# .ou can utili;e these features !hich
suites to your re(uirements#
%hanging Map 7ype
Google provides < "inds of map types 4or#al, 2y)rid, Satellite and 7errain# .ou can toggle to any "ind of
map using googl eMap. setMap7ype;< method#
google7a.set7aEyeQ6oogle7a.7-P<E=PE<IOR7-LR0
google7a.set7aEyeQ6oogle7a.7-P<E=PE<N=#RI.R0
google7a.set7aEyeQ6oogle7a.7-P<E=PE<S-EELLIEER0
google7a.set7aEyeQ6oogle7a.7-P<E=PE<EERR-IIR0
google7a.set7aEyeQ6oogle7a.7-P<E=PE<IOIER0
Showing %.rrent -ocation
.ou can sho! users current location on the map by calling setMy-ocati onEna)l ed;< # ,ass true = false
to enable or disable this feature
google7a.set7yLo;ationEnabledQtrueR0 "" 1alse to disable
>oo#ing =.ttons
.ou can call set>oo#%ontrol sEna)l ed;< function to get rid of those ;ooming controls on the map# &y
disabling these buttons map ;ooming functionality still !or" by pinching gesture#
google7a.getUiSettingsQR.set?oo3HontrolsEnabledQ1alseR0 "" true to enable
>oo#ing $.nctionality
.ou can disable ;ooming gesture functionality by calling set>oo#Gest.resEna)l ed;<
google7a.getUiSettingsQR.set?oo36esturesEnabledQ1alseR0
%o#pass $.nctionality
1ompass can be disabled by calling set%o#passEna)l ed;< function
google7a.getUiSettingsQR.setHo3assEnabledQtrueR0
My -ocation =.tton
My location button !ill be used to move map to your current location# $his button can be sho!n = hidden by
calling setMy-ocati on=.ttonEna)l ed;< function
google7a.getUiSettingsQR.set7yLo;ation#uttonEnabledQtrueR0
Map 8otate Gest.re
My rotate gesture can be enabled or disabled by calling set8otateGest.resEna)l ed;< method
google7a.getUiSettingsQR.setRotate6esturesEnabledQtrueR0
+lthough google maps provides lot of other features, I covered only basic topics in this tutorial# %emaining
topics seems to be pretty much lengthy, so Ill post them as separate articles#

Das könnte Ihnen auch gefallen