Android Tutorial Google Maps

Discussion in 'Tutorials & Examples' started by Erel, Feb 24, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This tutorial explains how to integrate Google Maps service in your app. It replaces the previous tutorial.
    It requires B4A v5.80+ and Android 4+.

    upload_2016-2-24_12-2-46.png
    (there are two Maps in this screenshot)

    Installation instructions:

    - Register your app in Google developer console: https://console.developers.google.com
    You need to enable Google Maps Android API.
    Then click on Credentials -> Create Credentials -> API Key -> Android Key.
    You should get a key that looks like: AIzaSyCmFHamGE0O0BvxxxxxxxxXbTCSrjFVg-Q

    - Copy the attached library (GoogleMaps v2) and put it in the additional libraries folder. Add a reference to the GoogleMaps library.

    - Make sure that the items listed here are installed: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/

    - Add Google Play Services Base snippet from the link above to the manifest editor.
    No need to add anything related to Firebase.

    - Add this snippet to the manifest editor (replace the value with your key):
    Code:
    AddApplicationText(
    <meta-data
      android:name=
    "com.google.android.geo.API_KEY"
      android:value=
    "AIzaSxxxxxxxxx "/>
    )
    - Add this line:
    Code:
    #AdditionalJar: com.google.android.gms:play-services-maps
    Don't add #AdditionalRes or #ExcludeClasses.
    If you are getting an error in the "Copying libraries resources" step then follow these instructions: https://www.b4x.com/android/forum/threads/google-maps.63930/page-4#post-432268

    - If you have never used Google Play Services before then you need to download it from Android SDK Manager and copy google-play-services.jar to the additional libraries folder.

    - Add this text to the manifest editor (replace the example key with your key):
    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=
    "AIzaSyCmFHamGE0O0BvxxxxxxxxXbTCSrjFVg-Q"/>
       <meta-data android:name=
    "com.google.android.gms.version"
       android:value=
    "@integer/google_play_services_version" />
    )
    -. Add these attributes to the main activity:
    Code:
    #AdditionalRes: $AndroidSDK$\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
    #ExcludeClasses: .games, .drive, .fitness, .wearable, .measurement, .cast, .auth, .nearby
    #ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .vision, .gcm
    'remove the following line if you use AdMob
    #ExcludeClasses: .ads

    - Set minSdkVersion to 14 (Android 4).

    - Add a MapFragment with the visual designer. It will appear under the CustomView menu. If you don't see it then make sure that the GoogleMaps library is selected.
    You can change the properties from the designer.
    Set the anchors to BOTH so the map will fill the activity:

    [​IMG]


    Implement the Ready event to get a reference to the GoogleMap object.
    Complete code:
    Code:
    #Region  Project Attributes
       
    #ApplicationLabel: B4A Example
       
    #VersionCode: 1
       
    #VersionName:
       
    'SupportedOrientations possible values: unspecified, landscape or portrait.
       #SupportedOrientations: unspecified
       
    #CanInstallToExternalStorage: False
        
    #AdditionalJar: com.google.android.gms:play-services-maps

    #End Region

    #Region  Activity Attributes
       
    #FullScreen: False
       
    #IncludeTitle: True
    #End Region

    'Activity module
    Sub Process_Globals

    End Sub

    Sub Globals
       
    Private gmap As GoogleMap
       
    Private MapFragment1 As MapFragment
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    Activity.LoadLayout("1")
       
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
         
    ToastMessageShow("Please install Google Play Services."True)
       
    End If
    End Sub

    Sub MapFragment1_Ready
       gmap = MapFragment1.GetMap
       
    Dim m1 As Marker = gmap.AddMarker(1030"test")
       m1.Snippet = 
    "This is the snippet"
    End Sub
     

    Attached Files:

    Last edited: Jan 11, 2017 at 3:30 PM
  2. fbritop

    fbritop Active Member Licensed User

    I have tried your example but it cannot compile:

    Invalid resource directory name: C:\SDK\Android\tools\..\extras\google\google_play_services\libproject\google-play-services_lib\res/drawable-xxxhdpi

    Part of the manifiest is:

    Code:
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "14" android:targetSdkVersion="14"/>
    <supports-screens android:largeScreens=
    "true" 
        android:normalScreens=
    "true" 
        android:smallScreens=
    "true" 
        android:anyDensity=
    "true"/>)

    AddApplicationText(<meta-data
      android:name=
    "com.google.android.maps.v2.API_KEY"
      android:value=
    "AIzaSyB2_c2ooYp_I9Q2r-XXXXXXXXX"/>
       <meta-data android:name=
    "com.google.android.gms.version"
       android:value=
    "@integer/google_play_services_version" />
    )
    key has been replaced with my console value
     
  3. rscheel

    rscheel Active Member Licensed User

    You need to place your api key.

    Code:
    android:value="Here's your api key"
     
  4. fbritop

    fbritop Active Member Licensed User

    Where it says:
    Code:
    android:value="AIzaSyB2_c2ooYp_I9Q2r-XXXXXXXXX"/>
    I already have put the correct API key from the console. I just print out with XXXXX no to make it public to the forum
     
  5. Daniel-White

    Daniel-White Active Member Licensed User

    Sorry, Perhaps I will do a stupid question, what is the improvement, or difference with the old tutorial.?
     
  6. ricardotm1968

    ricardotm1968 Member Licensed User

    Hi Erel,

    I'm trying to follow your example, follow the steps were as follows:
    1. android SDK-Update
    2. Copy google-play-services and android-support-v4 in additional libraries folder.
    3. Register my project in google and got the API key for android
    4. Copy the text to the manifest, replacing key API
    5. Copy the attributes
    6. Copy all code in an activity.
    When compiling displays the MapFragment but can not find the map
    What will be the error?
     
  7. vfafou

    vfafou Active Member Licensed User

    Hello!
    I have changed the mapfragment object as this tutorial.
    I'm using the GoogleMapsExtras library (AnimateToBounds) and I've noticed the following problem:
    When I'm in debug mode, it works normally.
    In release mode it does nothing. I can see the map but no movement!
    Is there any suggestion?

    Thank you in advance!
     
  8. DonManfred

    DonManfred Expert Licensed User

    It is now realized as customView (nees b4a 5.8+)
    Maybe other changes too but i don´t know :)
     
    Daniel-White likes this.
  9. Anser

    Anser Active Member Licensed User

    My app works fine on GoogleMaps Ver 1.01 but error out when I use ver 2.00, (I rechecked and confirmed that if I go back to ver 1.01, then it is working fine)

    I was using the Google Maps Ver 1.01 as per the old tutorial
    Today I changed to the new version of GoogleMaps ie ver 2.00

    The error is that the Map object is accessed before it is initialized, but the same code is working fine if I use ver 1.01

    Is there a minimum version for the Google Play Services to be used along with this version of GoogleMaps ?

    As of now I am using Google Play Services ver 28, I know that ver 29 is available, but haven't upgraded it as I read discussions regarding few bugs in Android Support Library and AppCompat etc. So I am waiting for a bug free version of Android Support Library. I believe that I can upgrade just the Google Play Service alone.

    I already have the API Key, I hope that I don't have to use a new API Key to use GoogleMaps ver 2.00


    Regards
    Anser
     
  10. MetalOS

    MetalOS Member Licensed User

    Hello,

    I followed the tutorial but the Google map remains white and no tile is loaded. here's a screenshot to show you the problem.


    [​IMG]
     
  11. DonManfred

    DonManfred Expert Licensed User

  12. MetalOS

    MetalOS Member Licensed User

    I had not seen this post. Thank you.
     
  13. MetalOS

    MetalOS Member Licensed User

    I solved my problem. I select my bad api developer console, now that I've selected the correct all works.
     
    DonManfred likes this.
  14. stanks

    stanks Active Member Licensed User

    in that Erel example from first post...how to remove marker and show another one on anther position?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  16. fbritop

    fbritop Active Member Licensed User

    Has someone has got this error and how to fix it?

    Code:
    B4A version: 5.80
    Parsing code.    (
    0.20s)
    Running custom action.    (
    0.04s)
    Compiling code.    (
    0.43s)
    Compiling layouts code.    (
    0.00s)
    Generating R 
    file.    Error
    invalid resource directory name: c:\sdk\android\extras\google\google_play_services\libproject\google-play-services_lib\res/drawable-xxxhdpi
    I have double checked than "c:\sdk\android\extras\google\google_play_services\libproject\google-play-services_lib\res" exists
    My manifiest has:
    Code:
    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
    Api key is correct

    Additional Libraries folder contains: android-support-v4.jar; GoogleMaps.jar; GoogleMaps.xml; google-play-services.jar; google-play-services.jar.properties

    Cannot seem to find where is the error. Any help?
     
  17. analizer3816

    analizer3816 Member Licensed User

    why mapfragment cannot set position(left,top,width,height) with code
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    Do you see this folder <sdk path>\extras\google\google_play_services\libproject\google-play-services_lib\res\drawable-xxxhdpi ?

    The correct way to set its layout is with the designer. MapFragment is not a view. If you need to move it at runtime then put it in a panel and move the panel.
     
  19. Rockefeller Goldman

    Rockefeller Goldman Active Member Licensed User

    I too don't have this Folder Erel, even after installing Google Play Services with AVD Manager.

    This ERROR appears: AndroidManifest.xml:33: error: Error: No resource found that matches the given name (at 'value' with value '@Integer/google_play_services_version').

    אנא עזור
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    AVD manager is not relevant. Try to update to the latest version of google play services. Make sure to later copy the updated jar file to the additional libraries folder.
     
    Rockefeller Goldman likes this.
Loading...