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:

GMan

Well-Known Member
Licensed User
Longtime User
i think there's a problem with the libs that i used in project, i don't know maybe they have conflict
Yo, so_ what libs are you using ?
 

arjian

Member
Licensed User
Longtime User
In the meantime,I checked them one by one and I found nothing, I'm almost desperate
 

arjian

Member
Licensed User
Longtime User
finally i found the problem,i changed the package name from uppercase to lowercase and problem solved but i don't know how is that possible.
 

GMan

Well-Known Member
Licensed User
Longtime User
but i don't know how is that possible.
AFAIK all used names for files, pictures etc. HAS TO BE lower case.

If you use i.e. a picture with Uppercase, B4A makes the name automatically lower case for internal use.
 

Rung

Member
Licensed User
Longtime User
Hi Martin,
I read many posts and tried many runs your examples #442(Example_1.zip) but the result is always White Screen.
I'd try my best from many suggestions for two days until now I can come across all problems as solved by you and your team experts
about GoogleMap Service New Revision. My manifest file, package name and API key setting are already passed.

Looking forward your help. My deadline is coming in few days.
Very much of thanks.
Rung.
 

Attachments

  • 1.AdditionRes.jpg
    1.AdditionRes.jpg
    26.1 KB · Views: 273
  • 2.API_KEY.jpg
    2.API_KEY.jpg
    171.9 KB · Views: 265
  • 3.Config Path.jpg
    3.Config Path.jpg
    81.6 KB · Views: 282
  • 4.Result.pdf
    269.1 KB · Views: 337

warwound

Expert
Licensed User
Longtime User
From the screenshot in Result.pdf i see the (blank) map with the zoom controller.
Your problem must be with the manifest entries and api key (as you suspected).

If you run your project again and uncheck the checkbox in the ide log panel - so as not to filter the logging - do you see anything relevant?
Look for anything logged that reference the google play services - something like 'api key check failed'.

Martin.
 

Rung

Member
Licensed User
Longtime User
Hi Erel and Martin,
My job is relating to using GoogleMap service. I've already Google member follow your all suggestions.
But it seem always show white screen in the result in my job.
Thanks a lot in advance.
Rung
 

warwound

Expert
Licensed User
Longtime User
This thread has many posts where the problems lies with the api key and properly including the current version of the google play services library.
To me it looks as if you have properly included the google play services library - if i was debugging your problem the api key is where i'd look.

Go to the Google APIs Console and check out the API access page - double (or triple) check that your key is valid for the Google Maps Android API v2 and that your manifest contains the correct key.

Have you changed your project's package name since you created the API key and not updated the API key with the new package name?

Martin.
 

Rung

Member
Licensed User
Longtime User
Thanks very much Martin. I already try it again with your comment.
Same result.
Rung
 

Attachments

  • 5.Log Screen at result uncheck filter.jpg
    5.Log Screen at result uncheck filter.jpg
    272.9 KB · Views: 285

Rung

Member
Licensed User
Longtime User
Martin. I use API key of my package name (not of Demo package name : uk.co.martinpearman.b4a.googlemapdemo)
I should create a new one for Demo package or not.

Rung
 

warwound

Expert
Licensed User
Longtime User
Hmmm i'm not sure what to check next.

In the IDE you have correctly selected the keystore (signing file) - the keystore used to create the api key?
(You're not still using the debug key).

Just saw you last reply, you need an api key created using the package name of the application that will use the key.
An api key created based on your application package name will not work with an example project that has a different package name.

Martin.
 

Rung

Member
Licensed User
Longtime User
Martin.
Last question for my very new member. I live in Asia. Can I use this code to show map in my home country?
If not , How to do that?

Rung
 

Rung

Member
Licensed User
Longtime User
Martin,
Can I apply the code in my app to show mark point and map (using my package name and API Key) or not?
If not , How do I?

Thank for your kind.
Rung
 

Rung

Member
Licensed User
Longtime User
Martin,
From #512 as you said "..An api key created based on your application package name...."
When I try to use my package name with my generated api key. I found that error massgae on my phone (connecting to
B4A-Bridge) can not install to the phone. My Log cat file as attached.
But when I use everthing the same exept using your application package name with api key (I create for it) it work fine, it's OK.
Why? Cannot run as I use Google LIb.with my coding by myself following your syntax as you guide in my Project.
If not How can I create by myself.

Thanks. Looking forward your guide again

RUNG
 

Attachments

  • 06.jpg
    06.jpg
    325.8 KB · Views: 254
  • LogCat connected to.pdf
    30.5 KB · Views: 347

warwound

Expert
Licensed User
Longtime User
I'm not sure what to try...

First in the IDE there's a 'clean project' option in one of the menus - click this.
That'll wipe out anything related to the old package name.

Now if you have already installed an app and then change it's package name then you need to uninstall the old version before you can install the new version.
If you use a usb cable to connect to your device then the uninstall and clean install should be automatic i think.
But with b4a bridge you might need to manually uninstall before clean installing with the new package name.

HTH.

Martin.
 

warwound

Expert
Licensed User
Longtime User
Also - You do realise that if you change the package name then you must generate an entirely new api key?

Martin.
 
Status
Not open for further replies.
Top