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

synapse

Member
Licensed User
Longtime User
Any idea, please, why using the example mapsforgetiledemo out of the box one gets a grey map base with zoom controls but no map? The program is executing ok?
 

marcick

Well-Known Member
Licensed User
Longtime User
The demo doesn't work as it is, did you modify the manifest with the correct API Key ?
 

synapse

Member
Licensed User
Longtime User
Yup. The key works with the the Googlemaps demo. The program is running to the end and have logged variables that seem to show results. Any other thoughts very much appreciated as hair being pulled out over this...
 

warwound

Expert
Licensed User
Longtime User
Did you also change the MapsForgeTileProvider demo package name to match the package name you used to obtain your API key?

If you look in the unfiltered log when the demo starts do you see anything relevant?

Martin.
 

marcick

Well-Known Member
Licensed User
Longtime User
The demo, as it is, works fine on my devices. Also without any change in the API key...
 

synapse

Member
Licensed User
Longtime User
That was it! Thanks. Does that mean one needs to apply for a key for each package name?
 

warwound

Expert
Licensed User
Longtime User
Read post #1 in this thread again.

You need an API key and the API key is based on the privtae sign key you use to sign your compiled app.
On the Google console you can add one or more application package names to that API key.

So it's easy to create an API key and use it for multiple applications (each with a different package name).

Martin.
 

synapse

Member
Licensed User
Longtime User
Have just been updated to 4.4 on a Nexus 7 which seems to have broken the example. No error but no longer any map, it seems!
 

synapse

Member
Licensed User
Longtime User
Authorization failure. Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.

Failed to contact Google servers. Another attempt will be made when connectivity is established.

At first glance might think I had set it up wrong, but it was working fine until the upgrade, and I have checked the values in the unfiltered logs against local and Google API that makes me think it is a handshake problem.

Elsewhere it says

ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());

Sorry to trouble you!
 

synapse

Member
Licensed User
Longtime User
Have generated another API key and made all changes to manifest & package; still no result...
 

warwound

Expert
Licensed User
Longtime User
I'm not sure what to suggest.
A short search on Google didn't reveal any problems with GoogleMaps and android 4.4.

You're compiling with the very latest version of Google Play Services are you and your Nexus is fully updated?
(There's no Android Map application update awaiting installation?).

Martin.
 

JohnC

Expert
Licensed User
Longtime User
How do you use "GetUiSettings" object?
 

warwound

Expert
Licensed User
Longtime User
The GoogleMap GetUiSettings method returns a MapUiSettings object which has the following methods and properties:

MapUiSettings
Methods:

  • IsInitialized As Boolean
Properties:
  • AllGesturesEnabled As Boolean [write only]
    Enables or disables all gestures.
  • CompassEnabled As Boolean
    Gets or sets whether the compass is enabled.
  • MyLocationButtonEnabled As Boolean
    Gets or sets whether the my-location button is enabled.
  • RotateGesturesEnabled As Boolean
    Gets or sets whether rotate gestures are enabled.
  • ScrollGesturesEnabled As Boolean
    Gets or sets whether scroll gestures are enabled.
  • TiltGesturesEnabled As Boolean
    Gets or sets whether tilt gestures are enabled.
  • ZoomControlsEnabled As Boolean
    Gets or sets whether zoom controls are enabled.
  • ZoomGesturesEnabled As Boolean
    Gets or sets whether zoom gestures are enabled.

So to enable display of the compass you'd do something like:

B4X:
GoogleMap1.GetUiSettings.CompassEnabled=True

The official documentation for the UiSettings class can be found here:
https://developers.google.com/maps/...erence/com/google/android/gms/maps/UiSettings.
Even though it's the java documentation it should still give you an idea of what each setting does.

Martin.
 

JohnC

Expert
Licensed User
Longtime User
Excellent - thanks for the info
 
Status
Not open for further replies.
Top