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

Sergio Haurat

Active Member
Licensed User
Longtime User
what I need is. Not manualy change the file attribute to read-only. the compiler left intact. if this is not possible, you have a choice B4A that the content of \res change it to RO before compiling and then back at the end (can be a sub option)

With the second option, the file without RO is deleted
 
Last edited:

Sergio Haurat

Active Member
Licensed User
Longtime User
With the second option, the file without RO is deleted

B4X:
#AdditionalRes: C:\Android\TestProject\Objects\res
 

Descartex

Well-Known Member
Licensed User
Longtime User
You have misconfigured something. Make sure that the libraries are in the correct place.

For any reason i ignore, when i've updated the SDK, the files of the Google Play Services has gone. I'm sure that i've updated it, not deleted it.
Reinstalling those packages the problem has solved. It might serve me for the futures crashes.

Thanks a lot.:D
 

Sergio Haurat

Active Member
Licensed User
Longtime User
Use:
B4X:
#AdditionalRes: C:\Android\TestProject\MyRes\res
Parsing code. 0.01
Compiling code. 0.11
Compiling layouts code. 0.02
Generating R file. Error
ERROR: resource directory 'C:\Android\TestProject\MyRes\res' does not exist

And the compiler is right, because there is no folder MyRes
 

Sergio Haurat

Active Member
Licensed User
Longtime User
I prefer to put the RO attribute to the file. Think if it is not a good idea, to version 3.5, adding options to change the attribute in the RO res folders., One sub-option would remove the RO attribute when the execution of the Release or Debug ends.
 

gvoulg

Member
Licensed User
Longtime User
Hi
I want to show custom map tiles (georeferenced imagery) that I have on my tablet's external memory in a directory structure that MapTiler creates(that is slippy map).
How should I construct the path for urltileprovider?
Thanks in advance for any help
George
 

warwound

Expert
Licensed User
Longtime User
Hi
I want to show custom map tiles (georeferenced imagery) that I have on my tablet's external memory in a directory structure that MapTiler creates(that is slippy map).
How should I construct the path for urltileprovider?
Thanks in advance for any help
George

Look at the GoogleMapsExtras CustomTileProvider: http://www.b4x.com/android/forum/threads/googlemapsextras.26277/page-3#post-198279
CustomTileProvider enables you to provide tiles to the map from your b4a code.

Example project attached.

Martin.
 

warwound

Expert
Licensed User
Longtime User
Hi Martin
Thanks for the example.
One more question.
How can I serve the titles from a directory in my sd card (internal or external)
The structure of the directory will be the slippy tile format.

I've attached a better example - this one shows how to create the Byte Array required by the Tile Object's Initialize method.
Tested and working on my Tab2 - my Tab2 has some OSMDroid/Mapnik cached map tiles on it's external memory.

Martin.
 

Attachments

  • CustomTileProvider-20140308.zip
    8.1 KB · Views: 356

gvoulg

Member
Licensed User
Longtime User
I've attached a better example - this one shows how to create the Byte Array required by the Tile Object's Initialize method.
Tested and working on my Tab2 - my Tab2 has some OSMDroid/Mapnik cached map tiles on it's external memory.

Martin.
Its working!
Thanks a lot Martin.
Do you think is possible to have Google maps , mapsForge (offline map ) and custom tiles from sd card defined in the same program?
Of course one of them to be visible every time.
George Voulgaridis
 

warwound

Expert
Licensed User
Longtime User
Its working!
Thanks a lot Martin.
Do you think is possible to have Google maps , mapsForge (offline map ) and custom tiles from sd card defined in the same program?
Of course one of them to be visible every time.
George Voulgaridis

That should work with no problems.
You'd create the MapsForge and CustomTile TileOverlays and use the TileOverlay Visible property to control which are visible.

Martin.
 

Porfix

New Member
Licensed User
Longtime User
Erel,

try to run the code in the first post however on my device with a floating window title appears: "Error analysis", message: "There was a problem parsing the package". No error messages in the log, and to compile the available modes. Can you help?
 

Porfix

New Member
Licensed User
Longtime User
I'm using a Xperia ZL with android version 4.3
APK mentioned already tried it and it works without any problem
 
Status
Not open for further replies.
Top