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

achtrade

Active Member
Licensed User
Longtime User
You just need to implement the sub:
B4X:
Sub Map_MarkerClick (SelectedMarker As Marker) As Boolean
   Log(SelectedMarker.Title)
   Return True
End Sub

Why the infowindows is no longer available after I added this sub ?
 

arnold steger

Member
Licensed User
Longtime User
How can convert from edittext to 2 separatly Double?
for example i have the text "46.810487,11.816784" in edittext.
I whant added a marker in my map at this position.

gmap.AddMarker2(Text.Latitude,Text.Longitude, "Position",gmap.HUE_RED)
 

eurojam

Well-Known Member
Licensed User
Longtime User
you can use the stringfunctions Lib, something like this:
B4X:
Dim l As List
Dim sf As StringFunctions
sf.Initialize
l = sf.Split(mystring,",")
gmap.AddMarker2(l.get(0),(l.get(1), "Position",gmap.HUE_RED)
not tested but it should work like this
cheers
stefan
 

arnold steger

Member
Licensed User
Longtime User
Thank eurojam for your help.
B4X:
Dim l As List
Dim l1,l2 As String
Dim sf As StringFunctions
sf.Initialize
l = sf.Split(SuchenEditText.Text,",")
l1=l.Get(0)
l2=l.Get(1)
gmap.AddMarker2(l1,l2, "Position",gmap.HUE_RED)
This code is work
 

shaxboz

Member
Licensed User
Longtime User
Try the attached APK. Does it work on your device?
Hi Erel!
Your apk is not working on my device.
Android 2.3
"The application B4A Example (process b4a.maps.example) has stopped Unexpectedly "
What should I do?
 

marcel

Active Member
Licensed User
Longtime User
When you click on a marker you will get a MarkerClick event and you can open the info window. Is their also an event when you click on this info window?
 

marcel

Active Member
Licensed User
Longtime User
Is this a limitation of the lib or is this not possible in Android?
 

FranckB

Member
Licensed User
Longtime User
Hi to All,

I have follow all off the steps to use Google Maps ( with GoogleMapsExtras as an extra lib ) in my app and all is running ok on a real device running 4.1.1 version.
( map is ok , marker is ok , ... )
But the map is blank on a device with 4.4.2 version of Android ?
Is there anything more to declare in the Manifest or anything else to do ( bad version of a lib , ) ?

Here is the 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="5" 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$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'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="AIzaSyCJD-[my personnal key masked]"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
    />)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.CALL_PHONE)

Thank's by advance
 

FranckB

Member
Licensed User
Longtime User
Hi Erel,

Thank's for your response.
Your APK is working on the 2 devices.
I have update the Google play services on the device but allready a blank map.
I have try to change the private key by a new one and i made the api key in the console.
Same problem with this new certificate...

Don't know how to do...
 

Croïd

Active Member
Licensed User
Longtime User
B4X:
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>

What did you write in your manifest , now ?

Are you sure for number key SHA , when you create API key ?
 

FranckB

Member
Licensed User
Longtime User
Hi Croïd

Yes I'm sure of the SHA key: the map work on another device ( same app version deployed on 2 devices : 1 work, the second one no)

Join a example project that working on a device and not on the other one
( Google play services is install on the 2 ones )
 

Attachments

  • testmap.zip
    11.5 KB · Views: 362

FranckB

Member
Licensed User
Longtime User
Hi Croïd,

You're right !
I have deleted and generate a new private key ( SHA-1 xxxx ) nd did all the procedure in the developper console of Google to have the correspondant API Key.
After reinstalling the app on the device... It's work !!!

Thank's a lot for your help !
 

jinxaw

Member
Licensed User
Longtime User
I have a problem with the "#AdditionalRes:". When i add that line to my code, the compiler crashes with:

Convert byte code - optimized dex. Error
UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: Java heap space

How can this happen?
Thank you very much in advance!

UPDATE:
I just realized that it's not "#AdditionalRes:..." line exclusively. But it is indeed something with the maps library. if i comment every evidence of maps-stuff out of my code (mfragment, gmap, manifest,...) the app works just fine...

UPDATE2:
As soon as i add the GoogleMaps library to my project, the error occurs while i try to compile my app.
 
Last edited:
Status
Not open for further replies.
Top