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:

Tony Tetley

Member
Licensed User
Longtime User
After completing the tutorial the app compiled clean, but when I run it in the emulator I get this:
java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist. 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" />
What I have in the manifest is what I got from the tutorial:
android:name="com.google.android.maps.v2.API_KEY"
android:value="A....my key....s8"/>
Do I need to add or change something? Thanks!
 

Augustinas Impalis

Member
Licensed User
Longtime User
I keep getting this error "Generating R file. Error Cannot find: C:\Users\Augustinas\AppData\Local\Android\android-sdk\tools\..\platform-tools\aapt.exe
Please configure paths (Tools - Configure Paths)." I dont understand what i need to do I looked at that directory and there's no such .exe file called aapt.exe, help please.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
also there is no com.google.android.gms in the \google-play-services_lib\res folder?
There should be any such file or folder (com.google.android.gms). This is the library package name.

The IDE looks for aapt.exe in several places. It is not clear why it finds it in other cases and fails here. You can manually copy it to:
C:\Users\Augustinas\AppData\Local\Android\android-sdk\platform-tools
 

Augustinas Impalis

Member
Licensed User
Longtime User
There should be any such file or folder (com.google.android.gms). This is the library package name.

The IDE looks for aapt.exe in several places. It is not clear why it finds it in other cases and fails here. You can manually copy it to:
C:\Users\Augustinas\AppData\Local\Android\android-sdk\platform-tools

Do you mean manually get aapt.exe and copy it to that path? if so I cannot find the file been looking everywhere
 

Augustinas Impalis

Member
Licensed User
Longtime User
There should be any such file or folder (com.google.android.gms). This is the library package name.

The IDE looks for aapt.exe in several places. It is not clear why it finds it in other cases and fails here. You can manually copy it to:
C:\Users\Augustinas\AppData\Local\Android\android-sdk\platform-tools

i am completely lost i searched all of my C: and Android folder for aapt.exe and no results i dont know where to look anymore, any suggestions?
 

Sergio Haurat

Active Member
Licensed User
Longtime User
My English is too basic

When you start the map the names are in English, which is the way to visualize, for example, in Spanish
 

fbritop

Active Member
Licensed User
Longtime User
Erel,
I have duplicated a project, but I keep getting an error:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0xd

I have tested the original application and it works OK, do you know were there should be some trouble?

I have generated the keystore file, with a new SHA fingerprint, and requested the API from google with a new package name in my other application. A have also tried with the old API Key, including a second allowed application at google, but no luck

Thanks
FBP
 

fbritop

Active Member
Licensed User
Longtime User
Yes, v3.20

Also the problem comes when using the AHViewer, in my second activity. Have tested in a panel in the main activity, and show OK!
 

fbritop

Active Member
Licensed User
Longtime User
Erel,
I have a strange behavior here.
There are 5 tabs (AHViewPager), the last one is the location tab.
Each tab content has it own panel (panX, where X goes 0->4)
Pan4 has a panelMap panel attach to it.

If the active tab is the location tab (#4) or the previous one, the gmail.Init runs OK, but if it is one of the others, it crashes, so in order to display, I need to have focus on the map tab, or either his neighbor

Why could this be happening?
 
Status
Not open for further replies.
Top