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

legendofjon

Member
Licensed User
Longtime User
Thanks for this thread.

The code is as follow
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region
#AdditionalRes: C:\Android\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#IncludeTitle: False

'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

with the following manifest
B4X:
'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" 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$")
'End of default text.

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="BC8L0XhM__CSpgQV2HCMAA-N"
    />)

I have also configure the paths to add an additional library at C:\Android\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs

however, despite these steps, I am receiving the below error and is confused.


Parsing code. 0.01
Compiling code. 0.04
Compiling layouts code. 0.02
Generating R file. 0.44
Compiling generated Java code. Error
B4A line: 45
gmap.AnimateCamera(cp)
javac 1.7.0_45
src\b4a\example\main.java:386: error: package com.google.android.gms.maps.model does not exist
mostCurrent._gmap.AnimateCamera((com.google.android.gms.maps.model.CameraPosition)(_cp.getObject()));
^
1 error

Kindly advise
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I have also configure the paths to add an additional library at C:\Android\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs
You should copy google-play-services.jar to your additional libraries folder. You do not need to set the additional libraries folder to the above folder.
 

legendofjon

Member
Licensed User
Longtime User
Thansk for your input. In my C:\Android\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs folder I have added the three followings
GoogleMaps xml
GoogleMaps jar file
google-play-services jar file
 

legendofjon

Member
Licensed User
Longtime User
I properly placed the files in their appropriate folder, and finally managed it to get it to run. However, when I run it, this error occurs - If mFragment.IsGooglePlayServicesAvailable = False Then and I am assuming its because my manifest file was not properly written. I have it written has
B4X:
'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" 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$")
'End of default text.

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="BC8L0XhM__CSpgQV2HCMAA-N"
    />)

Thanks for your inputs
 

Sergio Haurat

Active Member
Licensed User
Longtime User
I swear I already tried to find it. How to run the button on GoogleMaps, and take me to my position at runtime??
 

Sergio Haurat

Active Member
Licensed User
Longtime User
Martin, thanks for replying. I tried using this property but not found a way to catch the event, I write a generic example
Dim asd as GoogleMapsLibName
asd.MyLocationEnabled = True
asd.MyLocationCatch???("unknown")

Sub unknown_CatchEventOrWhatever(Latitude, Longitude, bla, bla, bla)
MsgBox("This is what I was looking for", "MyLocation")
End Sub

PS: Sorry for my English, I use the Google translator and possibly some things sound like Tarzan
 

susu

Well-Known Member
Licensed User
Longtime User
Is there any way to know when Marker Title clicked? (It's only MarkerClick for now)
 

susu

Well-Known Member
Licensed User
Longtime User
Yes, I did it. Thank you.

One more question: How to auto show Marker title when AnimateCamera into this Marker?
 

warwound

Expert
Licensed User
Longtime User
GoogleMapsExtras has various AnimateCamera? methods.

Look at AnimateCamera2 and AnimateCamera3 - they both accept a CancellableCallback parameter.
The CancellableCallback is executed when the user interupts the animation OR when the animation completes.

So you could use AnimateCamera2 or AnimateCamera3 to animate a camera change and once the animation completes use the GoogleMap Marker InfoWindowShown property to then show the infowindow.

Martin.
 

Descartex

Well-Known Member
Licensed User
Longtime User
Hi,
I have an error trying to execute the sample.

When my code gets to the following line:
B4X:
      mFragment.Initialize("Map", MapPanel)
the app raises an error and it closes.

java.lang.NoClassDefFoundError: anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment

I have no idea what's happening.

I have run the Erel's sample apk file and it works fine.

Thanks a lot.
 

Descartex

Well-Known Member
Licensed User
Longtime User
yeah. i'm using b4a 3.20 and i've copied the file and modified the manifest file adding the api key as described in this thread.
 

Sergio Haurat

Active Member
Licensed User
Longtime User
Dear Erel,
I'm testing the new version of B4A and I seem stable. I can make coexist and AndroidResources GoogleMaps, that's great.

Now, I'm happy!, I can use AndroidResources.

I have a question to ask you, I not have seen anything on the forum that can give me an explanation. When I compile, the application try to delete the file strings.xml?. There is possibility to avoid this?.

Thanks and congratulations for this new version
 
Status
Not open for further replies.
Top