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:



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:



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:



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:

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
please someone can explain me this error log?


B4X:
UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOfRange(Arrays.java:2694)
    at java.lang.String.<init>(String.java:203)
    at java.lang.String.substring(String.java:1877)
    at com.android.dx.rop.type.Prototype.withFirstParameter(Prototype.java:370)
    at com.android.dx.rop.type.Prototype.intern(Prototype.java:180)
    at com.android.dx.cf.iface.StdMethod.<init>(StdMethod.java:45)
    at com.android.dx.cf.direct.MethodListParser.set(MethodListParser.java:80)
    at com.android.dx.cf.direct.MemberListParser.parse(MemberListParser.java:217)
    at com.android.dx.cf.direct.MemberListParser.parseIfNecessary(MemberListParser.java:108)
    at com.android.dx.cf.direct.MethodListParser.getList(MethodListParser.java:54)
    at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:542)
    at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
    at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
    at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
    at com.android.dx.command.dexer.Main.processClass(Main.java:665)
    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)
    Optimized dexer failed. Switching to Standard dexer.
 

Powie

Member
Licensed User
Longtime User
After setting up a new pc I try to get my project back to work. First I try to get the maps demo to run as described in the first post.
It ends with these errors at compiling the code..

After reading many of the latest posts here I cannot find a solution. any suggestions? I think its related to google play services....
 

Attachments

  • Screenshot 2015-09-16 11.07.42.png
    14.9 KB · Views: 351

Powie

Member
Licensed User
Longtime User
It works with my old backed up google-play-services.jar

With the nactualwer google-play-services.jar this ist the complete output:



 

Powie

Member
Licensed User
Longtime User
Paths are correct. Use B4A 5.20

My project runs if I move the newer google-play-services.jar to the libraries folder and add to the manifest:

B4X:
<meta-data android:name="com.google.android.gms.version"
    android:value="7895000" />

In this case I don't use : #AdditionalRes: D:\android\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
If I add #AdditionalRes these errors occur.

Tryed it on a second PC, there is all running fine like described in the tutorial... Same Java Versions etc......
 

Powie

Member
Licensed User
Longtime User
I have setup a new PC Erel, so no older libraries are in place before.
 

AusSteve

Member
Licensed User
Longtime User
Folks,

I'm trying to implement the tutorial as written on
B4A version: 5.20
Parsing code. (0.00s)
Compiling code. (0.05s)
Compiling layouts code. (0.00s)
Generating R file. (0.24s)
Compiling generated Java code. (0.92s)
Convert byte code - optimized dex. (3.94s)
Packaging files. (1.41s)
Copying libraries resources (0.04s)
Signing package file (private key) (1.14s)
ZipAlign file. (0.17s)
Installing file to device. (0.09s)
Installing with B4A-Bridge.
Completed successfully.

Android Studio 1.3.2 JDK 1.8.0_31
Google Play Services v. 27 installed
Running Nexus 7 Android 5.1.1

I've followed the tutorial with one exception. when I tried to create the Android project I didn't get an option for a "Google Maps Android API v2" project, just google maps, and no options for "Simple API Access" , however, I got a key for the app.

Running for a debug
B4A version: 5.20
Parsing code. (0.00s)
Compiling code. (0.05s)
Compiling layouts code. (0.00s)
Generating R file. (0.24s)
Compiling generated Java code. (0.92s)
Convert byte code - optimized dex. (3.94s)
Packaging files. (1.41s)
Copying libraries resources (0.04s)
Signing package file (private key) (1.14s)
ZipAlign file. (0.17s)
Installing file to device. (0.09s)
Installing with B4A-Bridge.
Completed successfully.

Logcat shows:
Installing file followed by Activity Pause, and Activity Main

The device says "Parse Error There was a problem parsing the package"

I press OK and the device goes back to home screen, B4a is waiting for further action.

Now, when I open Android Studio, select the appname_debug.apk and open it in Android Studio, it compiles, installs to device and runs perfectly.

Any ideas?
 

AusSteve

Member
Licensed User
Longtime User
What happens if you connect to the device with USB debug mode?
When I run in USB Debug, the compile and debug dialog says :
B4A version: 5.20
Parsing code. (0.00s)
Compiling code. (0.12s)
Compiling layouts code. (0.00s)
Generating R file. (1.02s)
Compiling generated Java code. (2.26s)
Convert byte code - optimized dex. (6.27s)
Packaging files. (1.46s)
Copying libraries resources (0.06s)
Signing package file (private key) (1.18s)
ZipAlign file. (0.18s)
Installing file to device. Error
pkg: /data/local/tmp/WheelerCranesMapUtility_DEBUG.apk
Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]

Logcat
LogCat connected to: 015d43635d2c2410
--------- beginning of system
--------- beginning of main
Installing file.
Installing file.
android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Connected to B4A-Bridge (Wifi)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Installing file.
Installing file.
android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
Connected to B4A-Bridge (Wifi)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
** Activity (main) Create, isFirst = false **
running waiting messages (4)
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
sending message to waiting queue (CallSubDelayed - UpdateStatus)
running waiting messages (1)
** Activity (main) Resume **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
** Service (service1) Destroy **
** Service (service1) Create **
** Service (service1) Start **
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:googlemapsapi.wheelercranes.au.wheelercranesmaputility


Manifest Editor:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
AddManifestText( <permission
android:name="$PACKAGE$.permission.MAPS_RECEIVE"
androidrotectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>)
AddApplicationText(<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBhPHBaHenFlRjsE1tZuGZKlUCOGZs3-Ko"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version"/>)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
'End of default text.

Main Module
#Region Project Attributes
#ApplicationLabel: Utility
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: Landscape
#CanInstallToExternalStorage: False
#AdditionalRes: C:\correct_Path_ to_google-play-services_lib\res, com.google.android.gms
#End Region

Thanks for your quick response
 
Last edited:

AusSteve

Member
Licensed User
Longtime User
It is probably the forum parser fault. Next time use code tags and it will show correctly.

Can you upload your app (File - Export as zip)?
Ok, I'll figure code tags out next time.

I'll export in the morning our time, I am out of the office at the moment and wont get back today.

Thanks for your help, its much appreciated.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…