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

hatzisn

Expert
Licensed User
Longtime User
Hi everyone,

I downloaded version Google Play Services 29 from the link mentioned above and did everything I was supposed to do according to the first post.
Here is the error that I get. Am I missing a resource?

B4X:
Parsing code.  0.07
Compiling code.  0.36
  
ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code.  0.11
Generating R file.  Error
C:\B4A\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'.

And this is the XML included in the file:

B4X:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<!-- Base preview application theme. -->
<style parent="@android:style/Theme.Material.Light.DialogWhenLarge.NoActionBar" name="Theme.AppInvite.Preview.Base"/>
</resources>

What am I missing here? Have I done anything wrong?
 

hatzisn

Expert
Licensed User
Longtime User
I found a solution. It has happened before...
As Erel has explained in another post all you have to do is change the adroid.jar in your paths to a newer version
 

Startup

Active Member
Licensed User
Longtime User
I've now fixed the bug where Point could not be initialized, get the updated version of GoogleMapsExtras from here: http://www.b4x.com/forum/additional...pdates/26277-googlemapsextras.html#post152004.

And here's an example that logs the screen coordinates each time the map position changes:

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim GoogleMap1 As GoogleMap
   Dim GoogleMapsExtras1 As GoogleMapsExtras
    Dim MapFragment1 As MapFragment
    Dim MapPanel As Panel
   Dim Projection1 As Projection
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Google Play services not available.", True)
    Else
        MapFragment1.Initialize("MapFragment1", MapPanel)
    End If
End Sub

Sub MapFragment1_Ready
    Log("MapFragment1_Ready")
    GoogleMap1 = MapFragment1.GetMap
    If GoogleMap1.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(52, 0.75, 6)
        GoogleMap1.AnimateCamera(CameraPosition1)
    End If
End Sub

Sub MapFragment1_CameraChange (Position As CameraPosition)
   Projection1=GoogleMapsExtras1.GetProjection(GoogleMap1)
  
   Dim Point1 As Point
   Point1=Projection1.ToScreenLocation(Position.Target)
  
   Log("LatLng1: "&Position.Target)
   Log("Point1: "&Point1)
  
End Sub

On my Novo ElfII tablet that always logs (512, 246), the documentation states:



So i'm not sure how to use these coordinates, i'll have to leave you to experiment.

Martin.
 

Startup

Active Member
Licensed User
Longtime User
I have updated GoogleMapsExtras adding support for the OnInfoWindowClickListener.

Here's some example code:

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim GoogleMap1 As GoogleMap
   Dim Label1 As Label
    Dim MapFragment1 As MapFragment
    Dim MapPanel As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Google Play services not available.", True)
    Else
        MapFragment1.Initialize("MapFragment1", MapPanel)
    End If
End Sub

Sub MapFragment1_Ready
    Log("MapFragment1_Ready")
    GoogleMap1 = MapFragment1.GetMap
    If GoogleMap1.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
      GoogleMap1.MapType=GoogleMap1.MAP_TYPE_NORMAL
     
      Dim Coords() As Double=Array As Double(52.7667, 0.3931, 52.7480, 0.3918, 52.7550, 0.4026, 52.7667, 0.3931)
      Dim i As Int
      For i=0 To Coords.Length-2 Step 2
         Dim Marker1 As Marker
         Marker1=GoogleMap1.AddMarker(Coords(i), Coords(i+1), "Marker #"&(i/2))
         Marker1.Draggable=True
      Next
     
      Dim GoogleMapsExtras1 As GoogleMapsExtras
     
      Dim OnInfoWindowClickListener1 As OnInfoWindowClickListener
      OnInfoWindowClickListener1.Initialize("OnInfoWindowClickListener1")
     
      If OnInfoWindowClickListener1.IsInitialized Then
         GoogleMapsExtras1.SetOnInfoWindowClickListener(GoogleMap1, OnInfoWindowClickListener1)
      Else
         Log("OnInfoWindowClickListener1 is not initialized - check that the Activity contains a Click callback Sub")
      End If
     
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(52.75811, 0.3912, 13)
        GoogleMap1.AnimateCamera(CameraPosition1)
    End If
End Sub

Sub OnInfoWindowClickListener1_Click(Marker1 As Marker)
   Log("OnInfoWindowClickListener1_Click")
   Label1.Text=Marker1.Title&" : "&Marker1.Position
End Sub

3 Markers are added to the map, a click on a Marker shows an infowindow and a click on an infowindow causes the OnInfoWindowClickListener1_Click Sub to be called and a Label displays the position and title of the Marker that the infowindow was opened on.

Martin.

The next to last line in the example code --

B4X:
 Label1.Text=Marker1.Title&" : "&Marker1.Position

produces an error message --

"Object converted to String. This is probably a programming mistake"

can/should this be fixed or should it be ignored?
 

Almora

Well-Known Member
Licensed User
Longtime User
B4X:
Public Sub LocationChanged(Location1 As Location)
      Dim cp As CameraPosition
      cp.Initialize(Location1.Latitude, Location1.Longitude,14)
      gmap.AnimateCamera(cp)
      gmap.MyLocationEnabled=True

End Sub

I find the position on the map. But I can not go if I want to go somewhere else on the map. I'm back to the position again. How do you free yourself after finding your location?
 

Daniel Uribe

Member
Licensed User
Longtime User
Hi world!!

I have a little question. Is possible to remove the interest point of the maps?

You can see here.

and looking the screen..

Thanks a lot!!!
2012-05-29_1100.png
 

ivanomonti

Expert
Licensed User
Longtime User
I've now fixed the bug where Point could not be initialized, get the updated version of GoogleMapsExtras from here: http://www.b4x.com/forum/additional...pdates/26277-googlemapsextras.html#post152004.

And here's an example that logs the screen coordinates each time the map position changes:

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim GoogleMap1 As GoogleMap
   Dim GoogleMapsExtras1 As GoogleMapsExtras
    Dim MapFragment1 As MapFragment
    Dim MapPanel As Panel
   Dim Projection1 As Projection
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Google Play services not available.", True)
    Else
        MapFragment1.Initialize("MapFragment1", MapPanel)
    End If
End Sub

Sub MapFragment1_Ready
    Log("MapFragment1_Ready")
    GoogleMap1 = MapFragment1.GetMap
    If GoogleMap1.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(52, 0.75, 6)
        GoogleMap1.AnimateCamera(CameraPosition1)
    End If
End Sub

Sub MapFragment1_CameraChange (Position As CameraPosition)
   Projection1=GoogleMapsExtras1.GetProjection(GoogleMap1)
  
   Dim Point1 As Point
   Point1=Projection1.ToScreenLocation(Position.Target)
  
   Log("LatLng1: "&Position.Target)
   Log("Point1: "&Point1)
  
End Sub

On my Novo ElfII tablet that always logs (512, 246), the documentation states:



So i'm not sure how to use these coordinates, i'll have to leave you to experiment.

Martin.



Error
B4X:
B4A Versione: 7.30
Analisi del Codice.    (0.00s)
Compilazione del codice.    (0.03s)
Compilazione del codice di layouts    (0.02s)
Organizzazione Librerie.    (0.00s)
Generazione file R.    (0.06s)
Compilazione del codice Java prodotto.    Error
B4A line: 49
Projection1=GoogleMapsExtras1.GetProjection(Googl
javac 1.8.0_60
src\uk\co\martinpearman\b4a\googlemapdemo\main.java:368: error: package com.google.android.gms.maps does not exist
mostCurrent._projection1 = mostCurrent._googlemapsextras1.GetProjection((com.google.android.gms.maps.GoogleMap)(mostCurrent._googlemap1.getObject()));
                                                                                                    ^
1 error
 
Status
Not open for further replies.
Top