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,581
Last edited:

Rung

Member
Licensed User
Longtime User
Hi Matin,
After already uninstalled old version inside then I try as you guide, the result shown in my phone as attached.
I have no idea to deal with.

RUNG
 

Attachments

  • Screenshot_2014-08-03-22-36-59.png
    Screenshot_2014-08-03-22-36-59.png
    42.6 KB · Views: 317
  • 07.jpg
    07.jpg
    309.1 KB · Views: 318

Rung

Member
Licensed User
Longtime User
I use the existing api key of my package name which I generated (Copy from my API console) and paste in manifest file.
My package name is the same as shown after SHA1 code. I paste in my "Build Configuration Menu".

Thank a lot for your help soon.

RUNG
 

Attachments

  • 08.jpg
    08.jpg
    28.3 KB · Views: 270
  • 09.jpg
    09.jpg
    22.7 KB · Views: 298
  • 10.jpg
    10.jpg
    211.2 KB · Views: 321

Rung

Member
Licensed User
Longtime User
Hi All,
I found the solution. I just generate a new KeyStorefile with the new package name with word of 'com' infront of
my old package name 'BOH4.HMD4'. My new one is 'com.BOH4.HMD4' and then generate new API key for it for the
manifest file.
Maybe useful to anyone facing the same.

Rung
 

PhillipMorris

Member
Licensed User
Longtime User
Please check and tell me what am I doing wrong. Please...

B4X:
Parsing code.                          0.00
Compiling code.                        0.03
Compiling layouts code.                0.00
Generating R file.                      0.11
Compiling generated Java code.          0.36
Convert byte code - optimized dex.      1.11
Packaging files.                        0.82
Copying libraries resources            0.01
    Found 1 resource files.
Signing package file (private key)      0.43
ZipAlign file.                          Error
Cannot find:
Please configure paths (Tools - Configure Paths).
 

miguelconde

Member
Licensed User
Longtime User
Hello. I recevie an error with the SH1 code when use it in the "Configure Android Key for API Project" google services. Just say "Invalid input", i can not make the key. Do you know something a bout it. Thank you. I used the sh1 show in the "private sing key" option in b4a IDE.
 

miguelconde

Member
Licensed User
Longtime User
I am again :). I had a problem with the #AdditionalRes, i put my path res. C:\Android\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

the IDE show me a "error parsing atribute."

This my regio project attributes.
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: sfanet.móvil
#VersionCode: 1
#VersionName: 1.0.0
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: True
#AdditionalRes C:\Android\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

What i am doing wrong?

thank you.
 

miguelconde

Member
Licensed User
Longtime User
Finally I could install everything, but the application is reset when I call the statement:

If mFragment.IsGooglePlayServicesAvailable = True Then
mFragment.Initialize ("Map", MapPanel)
End If

"mFragment.IsGooglePlayServicesAvailable" hangs the application.
Does anyone have any idea may be happening?

I'm testing on an emulator. Genymotion with android 4.3. API 18, and the gps addon turn on.
 

miguelconde

Member
Licensed User
Longtime User
I performed the test with the example script on a real phone with android 4.4. I get the message "the application has stopped" and reset. in the same statement:
mFragment.IsGooglePlayServicesAvailable
 

miguelconde

Member
Licensed User
Longtime User
Erel, i did it with my phone, even in debug mode, i can't get the error, the application is broken and fall out. "unfortunately the application is stopped". I did change in the google api level, i changed the name package, got a new api ,etc, i dont know what more i can to do. May the manifest are wrong?. This is my manifest.

'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: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

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="AIzaSyA4YfBhk9rQVLPF4kcIJTDN4g9Mwf_9048"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
'SetApplicationAttribute(android:theme, "@android:style/Theme.NoTitleBar.Fullscreen")
'End of default text.
 

miguelconde

Member
Licensed User
Longtime User
That is, anything in the log, the application just fall and the OS show that message. "unfortunately the application is stopped". I created anew project with your example script, i run it in my phone an ocurred the same error. I will follow with the test tomorrow, i think the problem came from the api's that i installed, i have several api you can see in the image attachement. Api L, 20,19 and 18.
myapis.png
 

jhd

Member
Licensed User
Longtime User
Hi,

I use your api and social api and I encountered this error when I add GoogleMaps librairies

B4X:
Parsing code.                           0.01
Compiling code.                         0.09
Compiling layouts code.                 0.01
Generating R file.                      0.44
Compiling generated Java code.          1.13
Convert byte code - optimized dex.      Error
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ko;
    at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
    at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
    at com.android.dx.command.dexer.Main.processClass(Main.java:685)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
    at com.android.dx.command.dexer.Main.access$600(Main.java:78)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:596)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)
1 error; aborting
    Optimized dexer failed. Switching to Standard dexer.

Someone can solve this ?

I try the sample apk and it works on my device. Here s the problem it s compilation doesn t work

Thanx in advance
 

jhd

Member
Licensed User
Longtime User
I think it s because somme classes are already included in social API but i dont know how to remove them
 
Status
Not open for further replies.
Top