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

pal57

Member
Licensed User
You should update to a newer android.jar (22+) under Tools - Configure Paths.
Thankx Erel,
I updated to android.jar (23) and increased the dex memory and everything went fine, only for one thing.

It seems that I tried out your apk and then I disinstalled it before my compiled example.
It wont install my compile apk saying that there is an esisting package with the same name and a signuture in conflict al ready installed.

What did I did wrong.

Bye
Pal57
 

pal57

Member
Licensed User
Thankx Erel,
I updated to android.jar (23) and increased the dex memory and everything went fine, only for one thing.

It seems that I tried out your apk and then I disinstalled it before my compiled example.
It wont install my compile apk saying that there is an esisting package with the same name and a signuture in conflict al ready installed.

What did I did wrong.

Bye
Pal57

Sorry,
everything is working fine now.
Don't know why but I had two old apk of mine installed with the same name. I disinstalled them and now installed my new google map apk.

Thankx again Erel.
now I'll try to study on it.

Happy New Year :D

Pal57
 

Pablo Torres

Active Member
Licensed User
I cant get it to work well
It doesnt compile

this is the error:

B4A version: 5.50
Parsing code. (0.00s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Generating R file. Error
c:\users\pablo\desktop\android\extras\google\google_play_services\libproject\google-play-services_lib\res\values-v21\appinvite_styles.xml:5: error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.Material.Light.DialogWhenLarge.NoActionBar'.

can someone please help me?
 

Pablo Torres

Active Member
Licensed User
updated to sdk 23
now the error is:

B4A version: 5.50
Parsing code. (0.00s)
Compiling code. (0.41s)
Compiling layouts code. (0.05s)
Generating R file. (6.91s)
Compiling debugger engine code. (5.15s)
Compiling generated Java code. (7.25s)
Convert byte code - optimized dex. Error
UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.android.dx.util.ByteArray.slice(ByteArray.java:98)
at com.android.dx.cf.cst.ConstantPoolParser.parseUtf8(ConstantPoolParser.java:368)
at com.android.dx.cf.cst.ConstantPoolParser.parse0(ConstantPoolParser.java:262)
at com.android.dx.cf.cst.ConstantPoolParser.parse(ConstantPoolParser.java:150)
at com.android.dx.cf.cst.ConstantPoolParser.parseIfNecessary(ConstantPoolParser.java:124)
at com.android.dx.cf.cst.ConstantPoolParser.getPool(ConstantPoolParser.java:115)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:482)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
at com.android.dx.command.dexer.Main.parseClass(Main.java:764)
at com.android.dx.command.dexer.Main.access$1500(Main.java:85)
at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
at com.android.dx.command.dexer.Main.processClass(Main.java:749)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:718)
at com.android.dx.command.dexer.Main.access$1200(Main.java:85)
at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:672)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)

cant get it working
 

GMan

Well-Known Member
Licensed User
How exactly can i read the marker positions from a mysql-database ?
is rdc better or quicker as "simple" Google Maps ?
Is ALWAYS a script (php or java) needed on the server side or can i use the database directly from the app ?
 

Pablo Torres

Active Member
Licensed User
if is a MySQL database you can use Don Manfred MySQL Library (MSMYSQL) to connect directly, no server extra files
You can connect with webservices to any database, I recommend you this option, but need to install the webservice on the server
Both ways are super fast (If you are loading Name, Id, Latitude and Longitude for example, you can get 1000 records in 1-2 seconds)
 

GMan

Well-Known Member
Licensed User
Gracias Pablo,
as i think i used that lib some years ago - but forgot it (is this a programmers problem or mine only ? ) ;-)
Will use it again.

So another problem is this: actually i read out Lat & Long from a lblLat.Text and lblLon.Text.
No error occus while compiling and running, but no marker was shown ?
Here's the part i am using:
B4X:
Sub Map_Ready
   Log("map ready")
   gmap = mFragment.GetMap
   If gmap.IsInitialized = False Then
      ToastMessageShow("Error initializing map.", True)
   Else
      gmap.AddMarker(lblLat.Text , lblLon.Text, "Standort")
      Dim cp As CameraPosition
      cp.Initialize(lblLon.text, lblLat.text, gmap.CameraPosition.Zoom)
      gmap.AnimateCamera(cp)
   End If
End Sub

The format of that text is
lblLat.Text="51.1943"
lblLon.Text="6.29451"
 

Pablo Torres

Active Member
Licensed User
I use this code:
B4X:
     Dim EsteMarcador As Marker
     EsteMarcador=gMapLista.AddMarker (Estelocal.Latitud,Estelocal.Longitud,Estelocal.Nombre)
[\code]
instead the one you posted and it works fine for me, perhaps you can try that
 

Leni Berry

Active Member
Licensed User
dear erel,

how to show button zoom plus (+)/min(-) that own by gmap?
on my map, there is only locate button.
 

eurojam

Well-Known Member
Licensed User
My name is not Erel, but I can help you to - it is always better to ask all people in the forum then a specific person.
to enable the the zoombuttons you use the UISettings like this:
B4X:
GoogleMap1.GetUiSettings.ZoomControlsEnabled=True
 

Leni Berry

Active Member
Licensed User
My name is not Erel, but I can help you to - it is always better to ask all people in the forum then a specific person.
to enable the the zoombuttons you use the UISettings like this:
B4X:
GoogleMap1.GetUiSettings.ZoomControlsEnabled=True

thank u very much eurojam, it works.
i'm so sorry for not to ask all people in the forum.
 

Cebuvi

Active Member
Licensed User
Hello,

with google play services version 30, have disappeared the folders "\libproject\google-play-services_lib\res" had to be added in #AdditionalRes.


Which is now the path added in #AdditionalRes attribute?

Thanks.

César
 

scrat

Active Member
Licensed User
hello

I try to use B4a beta 6.0 with google maps. (this lib because i don't use designer)

I configured firebase and added google-services.json file to the root project path, updated the 4 sdk lib.
In my project i removed #AdditionalRes and #ExcludeClasses and added #AdditionalJar: com.google.android.gms: play-services-maps + Manifest snippets for google play services and firebase and the com.google.android.geo.API_KEY part.

Now my playservices version is V30

When I compile i have this error :

B4X:
B4A version: 6.00 BETA #1
Parsing code.  (1.19s)
Compiling code.  (1.65s)
Compiling layouts code.  (0.00s)
Generating R file.  (0.20s)
Compiling generated Java code.  (5.13s)
Convert byte code - optimized dex.  (9.94s)
Packaging files.  (1.64s)
Copying libraries resources  Error
Le fichier 'C:\Program Files (x86)\Anywhere Software\Basic4android-beta\libraries\google-play-services.jar' est introuvable. ("not found")

If I put google-play-service.jar V29 in my additional lib dir the project compile but use V29 lib

How to use googlemaps with V30 googleplay services ?

Thanks
 
Status
Not open for further replies.
Top