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:

klarsys

Active Member
Licensed User
Longtime User
Oh! Same silly mistake. Added API key but did not enable API for Android.

Thanks!
 

DarkAngel

Member
Licensed User
Longtime User
T

Thank you so much Martin,

That would be great, although the “googlemaps for work” it’s a paid version it would be great to have the extra functionalities available, like the road speed limit request.
This speed limit can be requested by HttpJob on b4a, but having this included on a library would be great.

There is another functionality that would be great to have on the library which is moving animatedly markers from one position (coordinates) to another, but this one I think it’s not included natively in the API. (Not sure about this, but I've seen Its possible).

If you could get this to work in a library like GoogleMapsExtras It would add even more functionality to the already excellent library.

Thank you very much in advance.


Bruno.
 

warwound

Expert
Licensed User
Longtime User
@DarkAngel

Well you'll still need b4a's GoogleMaps library re-compiled with the 'For Work' java package names.
You might want to make a request to Erel for the source code for the b4a GoogleMaps library.

I'll wait and see if Erel is prepared to make that source available.

I see it's easy to download the Google Maps Mobile SDK for Work: https://developers.google.com/maps/...ndroid/#download_with_the_android_sdk_manager
So it should be possible to download the SDK, re-compile both b4a GoogleMaps and GoogleMapsExtras and then leave you (with the API key) to test it all.
 

DarkAngel

Member
Licensed User
Longtime User
Great Martin,

I will make a request to Erel to see if that’s possible, and will feedback to you about it.

I’m just waiting the answer from google to pay the license to use the “for work” and then I will test it.
(I will use the speed limit request by httpjob even if Erel doesn’t make the source available for now)

Thank you for your help, much appreciated.
 

DarkAngel

Member
Licensed User
Longtime User
Hi Martin,

About last discussion, I'm searching for alternatives for speed limit on Google Maps for Work, the license I’s way to expensive…8.000€ a year…
So I won’t be able to test the code as I expected…

Thanks for your help anyway
 

Emmy

Member
Licensed User
Longtime User
Is there an updated version of this thread? I want to use the older version of google maps but the post is not clear as to what steps apply. Where can i download an working example?
 

juventino883

Member
Licensed User
Longtime User
Hi, I use BitmapDescriptor1=BitmapDescriptorFactory1.FromBitmap(LoadBitmap(File.DirAssets, "ht11.jpg")) for set a custom icon to a marker but when I run the app in my devices the Icon is clipped, I try to resize the images like said mikeH in this post https://www.b4x.com/android/forum/threads/googlemap-marker-icon-is-clipped.54373/ but in my case I still with the icon clipped :s some one know what ca I do to solve this?, thanks in advance.
 

klarsys

Active Member
Licensed User
Longtime User
This is issue with pixel density. Load bitmap specifying size in 'dip' to solve this issue.
 

foakgul

Member
Licensed User
Longtime User
Hello,

I followed all the instructions in the first message and when I try to compile my app, compiling hangs at: convert byte code - optimized dex
Then it times out and says:

The process is running longer than expected. Do you want to cancel it? and I click on no.

After a while:
Convert byte code - optimized dex. Error
Process timed out.
You can change the timeout value under Tools - IDE Options. Optimized dexer failed. Switching to Standard dexer.


The process still goes on, java.exe uses ~600MB RAM and ~90% CPU.

UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.android.dx.util.Bits.makeBitSet(Bits.java:38)
at com.android.dx.util.BitIntSet.<init>(BitIntSet.java:35)
at com.android.dx.ssa.SetFactory.makeLivenessSet(SetFactory.java:91)
at com.android.dx.ssa.SsaBasicBlock.addLiveOut(SsaBasicBlock.java:792)
at com.android.dx.ssa.back.LivenessAnalyzer.liveOutAtBlock(LivenessAnalyzer.java:199)
at com.android.dx.ssa.back.LivenessAnalyzer.handleTailRecursion(LivenessAnalyzer.java:141)
at com.android.dx.ssa.back.LivenessAnalyzer.run(LivenessAnalyzer.java:188)
at com.android.dx.ssa.back.LivenessAnalyzer.constructInterferenceGraph(LivenessAnalyzer.java:93)
at com.android.dx.ssa.back.SsaToRop.<init>(SsaToRop.java:82)
at com.android.dx.ssa.back.SsaToRop.convertToRopMethod(SsaToRop.java:69)
at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:101)
at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:72)
at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:299)
at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:139)
at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:94)
at com.android.dx.command.dexer.Main.processClass(Main.java:682)
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.

And the code does not compile.

This DOES NOT happen with other B4A codes. Only the sample Google Maps code.

Any thoughts as to what's going on?

Notes:
-Google Maps API key inserted into Manifest
-Code directly copied from 1st message on this thread
-Additional res file path specified

Thanks!
 

foakgul

Member
Licensed User
Longtime User
Thanks Erel. I found someone with the exact same problem and this:

You can increase the memory size that the dexer process is allowed to use.
Open the ini file that is located in:
C:\Users\<Your user name>\AppData\Roaming\Anywhere Software\Basic4android
Change this line:
MaxRamForDex=1024
To:
MaxRamForDex=1536


solved it!

 

boten

Active Member
Licensed User
Longtime User
Totally new to Maps, 2 preliminary questions concerning the first post:

as for step 1 Download Google Play services: under the Extra node, all I see are "Android support library" & "google usb driver" --- NO google play services

as for step 4 Create an API project: the link provided has a "Step 3. Create a Google Maps project" which assumes one installed Android Studio

Any hints on how to start fiddling with maps?
 

boten

Active Member
Licensed User
Longtime User
Disabled firewall, started SDK mngr, waited till "done loading pckgs" (the progress bar) - still no google play services under Extra
 

MbedAndroid

Well-Known Member
Licensed User
Longtime User
struggling with this command
#AdditionalRes <google-play-services res folder>, com.google.android.gms
the only com.google.android.gms map i could find on the pc is located here,
C:\Users\bert\AndroidStudioProjects\MyApplication\app\build\intermediates\exploded-aar

and gives a error during compiling

invalid resource directory name: C:\Users\bert\AndroidStudioProjects\MyApplication\app\build\intermediates\exploded-aar com.google.android.gms

googleplayservices is installed

any idea?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…