Android Maps V2 Quick Start

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

4/18/2014

Android Maps V2 Quick Start

Android Maps V2 Quick Start


Map Fragment / MapView v2 Quick Start The Maps APIs for Android have been updated in the latest release of the Google Play Services library and have been rolled out to Android devices with the Play Store. To test it out: 1. Create a new project a. File -> New -> Android Application Project b. You can leave all the options with their default values c. Choose BlankActivity in the Create Activity screen 2. Ensure you have the latest version (v2) of the Google Play Services installed. d. Select Window > Android SDK Manager or run android at the command line. e. Scroll to the bottom of the package list and select Extras > Google Play services. The Google Play services SDK is downloaded to your computer and installed in your Android SDK environment at < a n d r o i d s d k f o l d e r > / e x t r a s / g o o g l e / g o o g l e _ p l a y _ s e r v i c e s / 3. Add the Google Play Services project into your Eclipse workspace. f. Click File -> Import..., select Android -> Existing Android Code into Workspace g. Browse to and select < a n d r o i d s d k f o l d e r > / e x t r a s / g o o g l e / g o o g l e _ p l a y _ s e r v i c e s / l i b p r o j e c t / g o o g l e p l a y s e r v i c e s _ l i b 4. To add the dependency to Google Play Services into your project h. Project -> Properties -> Android -> Library, Add -> google-play-services_lib 5. Update M a i n A c t i v i t y . j a v a to extend F r a g m e n t A c t i v i t yinstead of A c t i v i t y 6. Check that Google Play Services is available on the device at runtime to protect against error cases
G o o g l e P l a y S e r v i c e s U t i l . i s G o o g l e P l a y S e r v i c e s A v a i l a b l e ( g e t A p p l i c a t i o n C o n t e x t ( ) ) ;

7. Update r e s / l a y o u t / a c t i v i t y _ m a i n . x m land replace the entire contents with < f r a g m e n tx m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d " a n d r o i d : i d = " @ + i d / m a p " a n d r o i d : l a y o u t _ w i d t h = " m a t c h _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " m a t c h _ p a r e n t " c l a s s = " c o m . g o o g l e . a n d r o i d . g m s . m a p s . S u p p o r t M a p F r a g m e n t " / > 8. The map is controlled through the G o o g l e M a pobject, use f i n d F r a g m e n t B y I dto keep a reference to the G o o g l e M a pfrom the layout.
G o o g l e M a pm a p=( ( S u p p o r t M a p F r a g m e n t ) g e t S u p p o r t F r a g m e n t M a n a g e r ( ) . f i n d F r a g m e n t B y I d ( R . i d . m a p ) ) . g e t M a p ( ) ;

To use Google Maps API you need an API Key: 1. Go to the Google APIs Console 2. In Services enable Google Maps Android API v2 and agree to the terms 3. In the left navigation bar, click API Access. 4. Click Create New Android Key.... 5. In the resulting dialog, enter the SHA-1 fingerprint, then a semicolon, then your application's package name. For example: a. BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample 6. If you have debug and production keys then add both in. b. To obtain the SHA1 of your debug certificate use keytool: k e y t o o ll i s ta l i a sa n d r o i d d e b u g k e yk e y s t o r e~ / . a n d r o i d / d e b u g . k e y s t o r es t o r e p a s s a n d r o i dk e y p a s sa n d r o i dv
https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw 1/2

4/18/2014

Android Maps V2 Quick Start

c. For Windows computers the .android directory is in your user directory d. Your new API key will be a 40 character string like A I z b S y B a E q e U p Y v l s b U e 0 O q T P n 1 A B V M H V b d N x o 7. Add the following tag into your A n d r o i d M a n i f e s t . x m ljust before the closing < / a p p l i c a t i o n > tag < m e t a d a t a a n d r o i d : n a m e = " c o m . g o o g l e . a n d r o i d . m a p s . v 2 . A P I _ K E Y " a n d r o i d : v a l u e = " y o u r _ a p i _ k e y " / > 8. Your application now also needs the following permissions in the A n d r o i d M a n i f e s t . x m l
< p e r m i s s i o n a n d r o i d : n a m e = " y o u r _ p a c k a g e _ n a m e . p e r m i s s i o n . M A P S _ R E C E I V E " a n d r o i d : p r o t e c t i o n L e v e l = " s i g n a t u r e " / > < u s e s p e r m i s s i o n a n d r o i d : n a m e = " y o u r _ p a c k a g e _ n a m e . p e r m i s s i o n . M A P S _ R E C E I V E " / > < u s e s p e r m i s s i o na n d r o i d : n a m e = " a n d r o i d . p e r m i s s i o n . I N T E R N E T " / > < u s e s p e r m i s s i o na n d r o i d : n a m e = " a n d r o i d . p e r m i s s i o n . W R I T E _ E X T E R N A L _ S T O R A G E " / > < u s e s p e r m i s s i o na n d r o i d : n a m e = " c o m . g o o g l e . a n d r o i d . p r o v i d e r s . g s f . p e r m i s s i o n . R E A D _ G S E R V I C E S " / >

9. Optionally you can include one or both of these permissions to enable auto-location
< u s e s p e r m i s s i o na n d r o i d : n a m e = " a n d r o i d . p e r m i s s i o n . A C C E S S _ C O A R S E _ L O C A T I O N " / > < u s e s p e r m i s s i o na n d r o i d : n a m e = " a n d r o i d . p e r m i s s i o n . A C C E S S _ F I N E _ L O C A T I O N " / >

10. Maps v2 uses OpenGl so the following uses-feature is also required


< u s e s f e a t u r ea n d r o i d : g l E s V e r s i o n = " 0 x 0 0 0 2 0 0 0 0 "a n d r o i d : r e q u i r e d = " t r u e " / >

11. Test your application, checking the logcat output for any errors. 12. If it is working try out some of the other GoogleMap options: a. m M a p . s e t M a p T y p e ( G o o g l e M a p . M A P _ T Y P E _ S A T E L L I T E ) ; b. m M a p . a d d M a r k e r ( n e wM a r k e r O p t i o n s ( ) . p o s i t i o n ( n e wL a t L n g ( 0 , 0 ) ) . t i t l e ( " M a r k e r " ) ) ; c. m M a p . s e t M y L o c a t i o n E n a b l e d ( t r u e ) ; d. more details at h t t p s : / / d e v e l o p e r s . g o o g l e . c o m / m a p s / d o c u m e n t a t i o n / a n d r o i d / v i e w s Attribution Requirements. If you use the Google Maps Android API in your application, you must include the Google Play Services attribution text as part of a "Legal Notices" section in your application. Including legal notices as an independent menu item, or as part of an "About" menu item, is recommended. The attribution text is available by making a call to G o o g l e P l a y S e r v i c e s U t i l . g e t O p e n S o u r c e S o f t w a r e L i c e n s e I n f o .

Published by Google Drive Report Abuse Updated automatically every 5 minutes

https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw

2/2

You might also like