Android Tutorial Google Maps Android v2 tutorial

Discussion in 'Tutorials & Examples' started by Erel, Dec 18, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    If you are using B4A v5.80+ then please follow this tutorial: https://www.b4x.com/android/forum/threads/google-maps.63930/#post-404386

    GoogleMaps library requires v2.50 or above.

    GoogleMaps library allows you to add Google maps to your application. This library requires Android 3+ and will only work on devices with Google Play service.

    This tutorial will cover the configuration steps required for showing a map.

    1. Download Google Play services - From the IDE choose Run AVD Manager and then choose Tools - SDK Manager. Select Google Play services under the Extras node and install it:

    [​IMG]

    2. Copy google-play-services.jar to the libraries folder - This file is available under:
    You should copy it to the libraries folder.

    2.5. Download the attached library, unzip it and copy to the libraries folder.

    3. Find the key signature - Your application must be signed with a private key other than the debug key. After you select a new or existing key file (Tools - Private Sign Key) you should reopen the private key dialog. The signature information will be displayed (increase the dialog size as needed).
    The value after SHA1 is required for the next step:

    [​IMG]

    4. Create an API project - Follow these steps and create an API project.
    You should follow the steps under "Creating an API Project" and "Obtaining an API key".

    Tips:
    - Make sure to select "Google Maps Android API v2" in the services list and not one of the other similar services.
    - Under "Simple API Access" you should select "Key for Android apps (with certificates".

    5. Add the following code to the manifest editor:
    Code:
    AddManifestText( <permission
              android:name=
    "$PACKAGE$.permission.MAPS_RECEIVE"
              android:protectionLevel=
    "signature"/>
          <uses-feature android:glEsVersion=
    "0x00020000" android:required="true"/>)

    AddApplicationText(<meta-data
        android:name=
    "com.google.android.maps.v2.API_KEY"
        android:value=
    "AIzaSyCzspmxxxxxxxxxxxxx"/>
    <meta-data android:name=
    "com.google.android.gms.version"
    android:value=
    "@integer/google_play_services_version"
        />)
    AddPermission(android.permission.ACCESS_NETWORK_STATE)
    You should replace the value after android:value with the key you received in the previous step.

    6. Add an #AdditionalRes attribute to the main activity:

    You should add a reference to Google play resources by adding the following line:
    Code:
    #AdditionalRes: <google-play-services res folder>, com.google.android.gms
    For example:

    #AdditionalRes: C:\android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

    Run the following code:
    Code:
    'Activity module
    Sub Process_Globals

    End Sub

    Sub Globals
       
    Dim mFragment As MapFragment
       
    Dim gmap As GoogleMap
       
    Dim MapPanel As Panel
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       MapPanel.Initialize(
    "")
       
    Activity.AddView(MapPanel, 00100%x100%y)
       
    If mFragment.IsGooglePlayServicesAvailable = False Then
          
    ToastMessageShow("Google Play services not available."True)
       
    Else
          mFragment.Initialize(
    "Map", MapPanel)
       
    End If
    End Sub
    Sub Map_Ready
       
    Log("map ready")
       gmap = mFragment.GetMap
       
    If gmap.IsInitialized = False Then
          
    ToastMessageShow("Error initializing map."True)
       
    Else
          gmap.AddMarker(
    3615"Hello!!!")
          
    Dim cp As CameraPosition
          cp.Initialize(
    3615, gmap.CameraPosition.Zoom)
          gmap.AnimateCamera(cp)
       
    End If
    End Sub
    You should see:

    [​IMG]

    If you see a "white map" then there is most probably a mismatch between the: package name, sign key and the API key (from the manifest editor).

    Google documentation: https://developers.google.com/maps/documentation/android/intro
    Note that there is a required attribution which you must include in your app (see above link). You can get the string by calling MapFragment.GetOpenSourceSoftwareLicenseInfo.

    V1.01: Fixes a bug in AddMarker2.
     

    Attached Files:

    Last edited: Feb 24, 2016
  2. NJDude

    NJDude Expert Licensed User

    Nice!!!
     
  3. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Nice :icon_clap:
    Just a few things...where did B4A v2.40 go?
    Google restricts map api access to 25,000 requests I think.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    This API doesn't have a usage limit: https://developers.google.com/maps/faq#usage_apis

    The next update is a large one. It deserves two points ;)
     
  5. warwound

    warwound Expert Licensed User

    Great - at last we'll have support for Google's MapView.

    I've been researching a bit and everything i've read says that it'll work on Froyo and later devices.
    Does that mean that the B4A implementation will work only on Honeycomb and later (for technical reaons maybe due to Fragment support?) or is there an error in the first post and the B4A implementation will work on Froyo and later?

    Anyone with a little time to spare might like to read this blog: Cyril Mottier » The Google Maps Android API v2 utopia.
    Written by a chap that's very familiar with the original version of the android maps library, he's not very positive and says at the end of the day the released version 2 is buggy and ought to have been a beta release.

    (Can't be any worse than all the current 3rd party mapping libraries i think - they tend to be an ongoing bug-fix).

    Martin.
     
  6. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Ah, nice, I should read more closely. Thanks.

    Oh, boy. I'm hearing lots of good things about this one!
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Yes. The implementation is based on fragments.
     
  8. rbsoft

    rbsoft Active Member Licensed User

    Just what I need for my next project...
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    The beta version is now available. If you like you can test it.
     
    sirai.soft likes this.
  10. bluedude

    bluedude Well-Known Member Licensed User

    Marker click event

    Hi Erel,

    Testing the map view and it works great and fast (loaded thousands of points). Is there a Marker click event?

    Cheers,
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    There is a click event. Check the events of MapFragment (write sub and then press on tab). I'm not in front of a computer so I cannot check it myself.
     
  12. bluedude

    bluedude Well-Known Member Licensed User

    Erel,

    Ok, found it and works.
     
  13. walterf25

    walterf25 Well-Known Member Licensed User

    Google API

    Hi Erel, i'm trying to update one of my apps, i need to include google maps, i'm following your instructions, everything runs great except i do not see the map just a white screen, i double checked the api key number and everything seems to be ok, i need your help, maybe you can figure out what i'm doing wrong?

    attached is an image showing my api key and the information extracted from my private sign key.

    SHA1.jpg

    and here's is my manifest file:

    hopefully you can give me some tips on how to fix this issue!

    Thanks,
    Walter
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Have you enabled the correct Google maps service?
     
  15. walterf25

    walterf25 Well-Known Member Licensed User

    Google Maps

    Hi Erel, thanks yeah i did not realize i had not installed google maps on my device, just installed it and everything works like a charm, thanks this is awesome!

    :sign0060:
     
  16. JTmartins

    JTmartins Active Member Licensed User

    Google Play Services

    I copied google-play-services executable jar file to internal libs of B4A.

    However no new lib appears in my B4A even after restarting B4A...What's missing ?

    Version 2.02 here

    Thanks

    Jose
     
  17. fboneol

    fboneol New Member Licensed User

    I have the same problem. the google play services lib is not listed.
     
  18. JTmartins

    JTmartins Active Member Licensed User

    GoogleMaps - v1.00

    I think this is what is missing The library GoogleMaps - v1.00, which I can't find to download, but it is mentioned in the documentation tab.

    Thanks

    José
     
  19. imbault

    imbault Well-Known Member Licensed User

    Same problem with the 2.5beta 4 I've just downloaded

    Patrick
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    The library is now attached to the first post...
     
Loading...