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:

jinxaw

Member
Licensed User
Longtime User
Thank you very much. It helped me to get the compiler to work.
Unfortunately another error was raised which i can't explain to myself.
The error occurs while the app is starting up.

B4X:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x5
    at android.content.res.Resources.getResourceName(Resources.java:2026)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    at android.app.BackStackRecord.run(BackStackRecord.java:833)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

My manifest contains the following lines:

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=<HASH>/>
    <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    )

AdditionalRes in Main Activity:

B4X:
    #AdditionalRes: ../maps_resources, com.google.android.gms

Everything i get from the googles are things like "trying to cast string in int" and stuff like that... but that cant be. am i the only one with that error?
i followed the tutorial step by step. including step #6. In my opinion it got stuck right there..
The worst thing is, that i', pretty sure that it's my fault but it's holding down my development process. Even worst - i'm going crazy about thar problem.
 
Last edited:

jinxaw

Member
Licensed User
Longtime User
The example works. I used the maps library in an older app and it still works fine there. But back in the older days i used to develop with b4a 3.X, so i did not include the additional res and so on...
I even can't get it to work without adding the additional res and without the needed lines in the manifest.

Another question: Schouldn't be all of those resource files inside of the "res" filder inside of the apk file? In my case, they are not.

I have come to the conclusion, that my problem is directly related to this one:
https://www.b4x.com/android/forum/t...-panels-now-perfect.14165/page-21#post-323219

Unfortunatelly, i have to replace the AHViewPager by another library.
 
Last edited:

BarryW

Active Member
Licensed User
Longtime User
Why i dont have a plus and minus sign on bottom right corner of my display? Tnx
 

BarryW

Active Member
Licensed User
Longtime User

Can we add a textbox to search location or places in here... I mean just like the google map that has a searchbox for places... Tnx
 

BarryW

Active Member
Licensed User
Longtime User

Can we add a textbox to search location or places in here... I mean just like the google map that has a searchbox for places... Tnx
 

DarkAngel

Member
Licensed User
Longtime User
Hello,

I’ve been using Google maps API on BA4 and would like to know if the B4A supports the use of Google maps roads. It seems to be a paid API which is Google Maps API for Work and uses the same code base as the standard Google Maps API.

Is there a way to implement this API with BA4 or use java script on B4A code?

It seems this Google Maps API for Work returns the speed limit of a specific road segment or given coordinates, and that’s what I searching to implement on an APP I’m working.

Or is there any alternative in case this won’t be possible to use with BA4?
 

Aviles

Member
Licensed User
Longtime User
Hello
My application works perfectly except wing loading google map, there are occasions that if it works the google maps and occasions that it gives me the following error
"api key not found".
please help
 

Aviles

Member
Licensed User
Longtime User
in the manifest this added all code needed.
i compile without changes and works great, loads the map,
but after a few days, show this error and stop working the map.

Note: included "NormalVersion" in the build config.

B4X:
Manifest....
AddPermission(android.permission.CHANGE_NETWORK_STATE)
AddPermission(android.permission.WRITE_SECURE_SETTINGS)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_NETWORK_STATE)

AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)

#if NormalVersion
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="xxxx-xxxxxxxxxxxxxxx" />
<meta-data android:name= "com.google.android.gms.version"
android:value= "@integer/google_play_services_version"
    />)
 

DarkAngel

Member
Licensed User
Longtime User
B4A GoogleMaps library is a wrapper above the free Android SDK. You can see the features available here: https://developers.google.com/maps/documentation/android/intro

For a different SDK, you will need to create a new wrapper.


I see,

So I would need to create a library for the "Google Maps Mobile SDK for Work"...

In this case i would need to write some java code to use the features I need from this google api?
And add it to the manifest to?

Thanks for the reply.
 

warwound

Expert
Licensed User
Longtime User



I see,

So I would need to create a library for the "Google Maps Mobile SDK for Work"...

In this case i would need to write some java code to use the features I need from this google api?
And add it to the manifest to?

Thanks for the reply.

https://developers.google.com/maps/...ct_gmm_sdk_for_work_with_google_play_services

Looks like both APIs have the same syntax but different java package names.
And the "for Work" API presumably has a few extra classes.

So if you had the source code for either/both GoogleMaps and GoogleMapsExtras libraries then you could change the API package names and recompile and have a working b4a "for Work" library.
You're welcome to a copy of GoogleMapsExtras source code - let me know if you want it.

Martin.
 

klarsys

Active Member
Licensed User
Longtime User
I get this error frequently while rotating the device and then the app crashes:
B4X:
** Activity (meetme) Resume **
map ready
** Activity (meetme) Pause, UserClosed = false **
** Activity (meetme) Create, isFirst = false **
** Activity (meetme) Resume **
map ready
** Activity (meetme) Pause, UserClosed = false **
java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll()
    at java.lang.Object.notifyAll(Native Method)
    at com.google.maps.api.android.lib6.gmm6.m.k.a(Unknown Source)
    at com.google.maps.api.android.lib6.gmm6.m.l.run(Unknown Source)
** Activity (meetme) Create, isFirst = false **
** Activity (meetme) Resume **
** Activity (meetme) Pause, UserClosed = true **
java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll()
    at java.lang.Object.notifyAll(Native Method)
    at com.google.maps.api.android.lib6.gmm6.m.k.a(Unknown Source)
    at com.google.maps.api.android.lib6.gmm6.m.l.run(Unknown Source)

What could be wrong?

Also, I get the white map. I double-checked SHA1 key, package name and API key. Everything seems to be correct. What else could go wrong? How do I debug it?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…