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:

Cebuvi

Active Member
Licensed User
Longtime User
Hi,
Two months ago I wrote an application using Google Maps api. It worked perfectly in both the emulator and the phone.
Yesterday I went to run it from BA4 version 4.3 and I get the following error in emulator and the phone. Where is the problem?

java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 5089000 but found 6587000. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@Integer/google_play_services_version" />

at com.google.android.gms.common.GooglePlayServicesUtil.A(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
at anywheresoftware.b4a.objects.MapFragmentWrapper.IsGooglePlayServicesAvailable(MapFragmentWrapper.java:101)
at com.rwy04apps.mapa1.main._activity_create(main.java:337)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at com.rwy04apps.mapa1.main.afterFirstLayout(main.java:100)
at com.rwy04apps.mapa1.main.access$100(main.java:17)
at com.rwy04apps.mapa1.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 5089000 but found 6587000. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@Integer/google_play_services_version" />


Thanks.

César


 

Attachments

  • Mapa1.zip
    12 KB · Views: 316

warwound

Expert
Licensed User
Longtime User

A common cause for this error is:
  • You updated your android SDK - the google-play-services.jar in your android SDK has been updated.
  • Your b4a additional libraries folder contains an older version of google-play-services.jar.

Fix: Copy the (newer) google-play-services.jar from your android SDK folder to your b4a additional libraries folder.
 

Cebuvi

Active Member
Licensed User
Longtime User

thanks for the reply, I updated the library but still the same error
 
Last edited:

Cebuvi

Active Member
Licensed User
Longtime User
Hi Erel,
If I change the manifest and write this :

<meta-data android:name="com.google.android.gms.version"

android:value="5089000" />)

Where "5089000" is the value that the log was saying it expected, everything works.

But why it now fails if with the code shown in point #5 before work?

Every time version of Google Play is updated, I'll have the same problem?

Thanks.

César
 

Cebuvi

Active Member
Licensed User
Longtime User
You shouldn't set the explicit version.

I'm 99% sure that your google-play-services.jar (the one in the additional libraries folder) doesn't match the res folder you added with #AdditionalRes.

Hi Erel,

I uninstalled Android SDK, I uninstalled B4A. I have installed the latest version of Android SDK on a new path directly in C :, I installed B4A, I configured everything and I changed the reference correctly in #AdditionalRes and I still have the same problem.

Any other ideas?

Thanks

César
 

Cebuvi

Active Member
Licensed User
Longtime User
Problem solved.

I had copied google-play-services.jar in the additional libraries folder and still had the error. I've copied in the libraries folder and now everything works.

Thank you very much and sorry for so much work

César
 

hanyelmehy

Active Member
Licensed User
Longtime User
when try to call
B4X:
mFragment.IsGooglePlayServicesAvailable
return false
notes :
google play services is updated
google-play-services.jar (the one in the additional libraries folder) match the res folder i added with #AdditionalRes.
your GoogleMaps.apk is work
i used android 13
what i am doing wrong??
 

hanyelmehy

Active Member
Licensed User
Longtime User
What happens if you skip this check?
The phone is ask to update Google play service ,when update its work
Is That mean ,Google play service must be updated on target devices ?not enough to use last Google play service in B4A project ??
is there is any way to check Google play service version on target device ,and check if its need update or not ?
 

achtrade

Active Member
Licensed User
Longtime User
you were right, I think the marker is too small for my finger

I need the new position (lat,lon) after drag the marker, is there something like this ?

B4X:
Sub Marker_DragEnd(NewLatitude AsString, NewLongitude AsString)' only String types can be passed from the javascript to B4A so we need to convert the String types to Double
Dim Latitude As Double=NewLatitude
Dim Longititude As Double=NewLongitude
Log("The user has dragged the Marker to ("&Latitude&", "&Longititude&")")
End Sub
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…