Android Tutorial (old) Google Maps Android v2 tutorial

Status
Not open for further replies.
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:

SS-2012-12-18_18.28.04.png


2. Copy google-play-services.jar to the libraries folder - This file is available under:
C:\<android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\libs (ignore the extra space that the forum script insists on adding)
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:

SS-2012-12-18_18.11.34.png


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:
B4X:
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:
B4X:
#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:
B4X:
'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, 0, 0, 100%x, 100%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(36, 15, "Hello!!!")
      Dim cp As CameraPosition
      cp.Initialize(36, 15, gmap.CameraPosition.Zoom)
      gmap.AnimateCamera(cp)
   End If
End Sub

You should see:

SS-2012-12-18_18.25.14.png


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.
 

Attachments

  • GoogleMaps.zip
    17.8 KB · Views: 11,592
Last edited:

mattdarnell

Member
Licensed User
Longtime User
Precompiled APK will start but there is no marker, is this normal for a Note 4?

Google play services is installed and there maps appears updates.

Thanks
 

Attachments

  • 2015-12-08 00.04.13.png
    2015-12-08 00.04.13.png
    193.6 KB · Views: 577
  • 2015-12-08 00.26.05.png
    2015-12-08 00.26.05.png
    201 KB · Views: 530
  • 2015-12-08 00.03.57.png
    2015-12-08 00.03.57.png
    182.7 KB · Views: 515

Indy

Active Member
Licensed User
Longtime User
Hi,

I don't know if I've done something wrong or left something out but, whenever I try and compile a project with maps the dialog just stops at
B4X:
Convert byte code - optimized dex.

Eventually after the timeout occurs I get the following message.
B4X:
UNEXPECTED TOP-LEVEL ERROR:

java.lang.OutOfMemoryError: GC overhead limit exceeded
I have already increased the timeout from 60s to 180s. Any ideas where I've gone wrong?

Thanks
 

Indy

Active Member
Licensed User
Longtime User
Hi All,

Does this library have a Zoom In/Out event? If not, how can this be acheived? I have the map up and running ad set an initial zoom value of 17 but, I would like to retain the value of the zoom when the Lat/Long is set manually.

Thanks
 

pal57

Member
Licensed User
Longtime User
Hi,
I have this error while compiling from B4A:


B4A version: 5.50
Parsing code. (0.00s)
Compiling code. (0.03s)
Compiling layouts code. (0.00s)
Generating R file. Error
c:\android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res\values-v21\appinvite_styles.xml:5: error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.Material.Light.DialogWhenLarge.NoActionBar'.

My Manifest is:

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
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.gms.version"
android:value="@Integer/google_play_services_version" />)
AddApplicationText(<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBIclOSVBCL9by21c7Siz8mYOyp5I_7M_w"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version"
/>)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.



I downloaded your apk and it all works fine.

Don't know what am I doing wrong.

Thankx
Pal57
 
Last edited:

pal57

Member
Licensed User
Longtime User
Sorry but this is the real Manifest in my apk:

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
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.gms.version"
android:value="@Integer/google_play_services_version" />)
AddApplicationText(<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBIclOSVBCL9by21c7Siz8mYOyp5I_7M_w"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version"
/>)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.

Pal57
 

pal57

Member
Licensed User
Longtime User
Sorry but don't know why when I copy and past my manifest text, it shows up all messed up in my reply.
Pal57
 

pal57

Member
Licensed User
Longtime User
Here I attach my manifest file in png format.

Sorry for the trouble.

Pal57
 

Attachments

  • Manifest.PNG
    Manifest.PNG
    52.3 KB · Views: 488

pal57

Member
Licensed User
Longtime User
You should update to a newer android.jar (22+) under Tools - Configure Paths.
Thankx Erel,
I updated to android.jar (23) and increased the dex memory and everything went fine, only for one thing.

It seems that I tried out your apk and then I disinstalled it before my compiled example.
It wont install my compile apk saying that there is an esisting package with the same name and a signuture in conflict al ready installed.

What did I did wrong.

Bye
Pal57
 

pal57

Member
Licensed User
Longtime User
Thankx Erel,
I updated to android.jar (23) and increased the dex memory and everything went fine, only for one thing.

It seems that I tried out your apk and then I disinstalled it before my compiled example.
It wont install my compile apk saying that there is an esisting package with the same name and a signuture in conflict al ready installed.

What did I did wrong.

Bye
Pal57

Sorry,
everything is working fine now.
Don't know why but I had two old apk of mine installed with the same name. I disinstalled them and now installed my new google map apk.

Thankx again Erel.
now I'll try to study on it.

Happy New Year :D

Pal57
 

Pablo Torres

Active Member
Licensed User
Longtime User
I cant get it to work well
It doesnt compile

this is the error:

B4A version: 5.50
Parsing code. (0.00s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Generating R file. Error
c:\users\pablo\desktop\android\extras\google\google_play_services\libproject\google-play-services_lib\res\values-v21\appinvite_styles.xml:5: error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.Material.Light.DialogWhenLarge.NoActionBar'.

can someone please help me?
 
Status
Not open for further replies.
Top