android working with google maps v2

Upload: stefanive

Post on 02-Jun-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Android Working With Google Maps V2

    1/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 Google P lay Services )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 Managerand chec" !hether you have already do!nloaded

    Google lay )ervices or not under E!trassection# If not select play services and install the pac"age#

    http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/
  • 8/11/2019 Android Working With Google Maps V2

    2/26

    2. "#porting Google Play Services into Eclipse

  • 8/11/2019 Android Working With Google Maps V2

    3/26

    +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# lic" 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#

  • 8/11/2019 Android Working With Google Maps V2

    4/26

  • 8/11/2019 Android Working With Google Maps V2

    5/26

    +. Getting the Google Maps AP" key

    1# )ame as in maps v1 !e need to generate )*+1 fingerprint using 'ava keytool # 0pen your terminal and

    eecute the follo!ing command to generate )*+1 fingerprint#

    ,n Windowskeytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore"-alias androiddebugkey -storeass android -keyassandroid

    ,n -in! or Mac ,S

    keytool -list -v -keystore !.androiddebug.keystore -alias androiddebugkey -storeass android -keyass android

    In the output you can see )*+ 1 finger print#

  • 8/11/2019 Android Working With Google Maps V2

    6/26

    2# 4o! open Google +I onsole

    +# )elect Serviceson left side and turn on Google Maps Android AP" v2

    /# 4o! select AP" Accesson left side and on the right side clic" on %reate new Android key0

  • 8/11/2019 Android Working With Google Maps V2

    7/26

    # It !ill popup a !indo! as"ing the )*+1 and pac"age name# /nter your SA 1 and your androidpro*ect package na#e separated by semicolon 3 and clic" on create#

  • 8/11/2019 Android Working With Google Maps V2

    8/26

    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#

  • 8/11/2019 Android Working With Google Maps V2

    9/26

    /. %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*ectand fill re(uired

    details# I "ept my pro'ect name as Google Maps V2 and pac"age name as in5o.androidhive.in5o

    2# 4o! !e need to use Google lay )ervices pro'ect as a library to use pro'ect# )o right clickon pro'ect

    and select properties# In the properties !indo! on left side select Android# 0n the right you can see

    aAddbutton under library section# lic" it and select google play servicespro'ect !hich !e imported

    previously#

  • 8/11/2019 Android Working With Google Maps V2

    10/26

  • 8/11/2019 Android Working With Google Maps V2

    11/26

  • 8/11/2019 Android Working With Google Maps V2

    12/26

    +# +dd the Map -ey in the manifest file# 0pen AndroidMani5est.!#lfile and add the follo!ing code

    before tag# %eplace the android6vale!ith your map "ey !hich you got from google console#

    45-- 6oolge 7as PI 8ey --943eta-data

  • 8/11/2019 Android Working With Google Maps V2

    13/26

    android$na3e:";o3.google.android.3as.v,.PIaSy#?7lkOv(s@-7ABO)/CksdaD(E@Lgo"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 statusW8"7E(E97E84A-(S7,8AGE5 $o !rite to eternal storage as google maps store map data in

    eternal storage

    A%%ESS(%,A8SE(-,%A7",45 $o determine users location using 6i7i and mobile cell data

    A%%ESS($"4E(-,%A7",45 $o determine users location using G)

    ,penG- ES V2 5 %e(uired for Google Maps V2

    7inally my AndroidMani5est.!#lfile loo"s li"e this 8%eplace the pac"age name !ith your pro'ect

    pac"age9

    AndroidManifest.xml4GD3lversion:"'."en;oding:"ut1-+"G943ani1estD3lns$android:"2tt$s;2e3as.android.;o3akresandroid" a;kage:"in1o.android2ive.google3asv," android$versionHode:"'"

    android$versiona3e:"'."94er3ission

    android$na3e:"in1o.android2ive.google3asv,.er3ission.7PS

  • 8/11/2019 Android Working With Google Maps V2

    14/26

    4uses-er3issionandroid$na3e:"android.er3ission.JRIE

  • 8/11/2019 Android Working With Google Maps V2

    15/26

    41rag3ent android$id:"Mid3a" android$na3e:";o3.google.android.g3s.3as.7aFrag3ent" android$layout

  • 8/11/2019 Android Working With Google Maps V2

    16/26

    MOverride

    rote;tedvoidonResu3eQ suer.onResu3eQ0 initili>e7aQ0

    %un your pro'ect and congratulations if you see a map displaying on your device#

  • 8/11/2019 Android Working With Google Maps V2

    17/26

  • 8/11/2019 Android Working With Google Maps V2

    18/26

    Placing a Marker

    .ou can place a mar"er on the map by using follo!ing code#

    latitude and longitudedoublelatitude : 0doublelongitude : 0

    ;reate 3arker7arkerOtions 3arker : neC7arkerOtionsQ.ositionQneCLatLngQlatitude longitude.titleQ"Nello 7as "0

    adding 3arkergoogle7a.add7arkerQ3arker0

  • 8/11/2019 Android Working With Google Maps V2

    19/26

  • 8/11/2019 Android Working With Google Maps V2

    20/26

    %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;on3arker.i;onQ#it3aes;ritorFa;tory.de1ault7arkerQ#it3aes;ritorFa;tory.NUE

  • 8/11/2019 Android Working With Google Maps V2

    21/26

    %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#As se t; String asset4a#e< 5 :oading from assets folder

    5ro#=it#ap ;=it#ap i#age< 5 :oading bitmap image

  • 8/11/2019 Android Working With Google Maps V2

    22/26

    5ro#$ile ;String path< 5 :oading from file

    5ro#8esorce ;int resorce"d< 5 :oading from dra!able resource

    &elo! I loaded a custom mar"er icon from drawa)lefolder

    latitude and longitude

    doublelatitude : '*.&+A((0doublelongitude : *+.(+//*'0

    ;reate 3arker7arkerOtions 3arker : neC7arkerOtionsQ.ositionQneCLatLngQlatitude longitude.titleQ"Nello 7as"0

    H2anging 3arker i;on3arker.i;onQ#it3aes;ritorFa;tory.1ro3Resour;eQR.draCable.3y

  • 8/11/2019 Android Working With Google Maps V2

    23/26

    %hanging Map 7ype

    Google provides < "inds of map types 4or#al, y)rid, Satelliteand 7errain# .ou can toggle to any "ind of

    map using googleMap.setMap7ype;< method#

    google7a.set7ayeQ6oogle7a.7P

  • 8/11/2019 Android Working With Google Maps V2

    24/26

  • 8/11/2019 Android Working With Google Maps V2

    25/26

    Showing %rrent -ocation

    .ou can sho! users current location on the map by calling setMy-ocationEna)led;oo#ing =ttons

    .ou can call set>oo#%ontrolsEna)led;

  • 8/11/2019 Android Working With Google Maps V2

    26/26

    google7a.getUiSettingsQ.setHo3assEnabledQtrue0

    My -ocation =tton

    My location button !ill be used to move map to your current location# $his button can be sho!n = hidden bycalling setMy-ocation=ttonEna)led;