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:

jhd

Member
Licensed User
Longtime User
I open both zip (eslocation2 and googlemap) using 7zip and i haven t duplicate classes.

I think the duplicate is on gplussdk.jar from social api but all classes names look obfuscate :/

In your google map api there is a class named GooglePlusProvider.class

In social.jar there is this class too. I try to delete it this night.

Thanx for your help, I think it solved my problem
 
Last edited:

hzchrisfang

Member
Licensed User
Longtime User
Are you testing it on an emulator?
If you are testing it on a real device then you need to go to Google Play and install Google Play Services.

I test it on a real device, and I have install Google Play Services already.
 

Attachments

  • Screenshot_2014-09-23-13-48-18.png
    Screenshot_2014-09-23-13-48-18.png
    212.9 KB · Views: 299

Noize

Member
Licensed User
Longtime User
Is it possible to show the "google maps" view inside an app?

I'm thinking on show users using my app inside my app, and the ability to geolocalize other users, like many other apps on the market,

Cheers
 

Noize

Member
Licensed User
Longtime User
I've added a Panel with the designer on a TabHost with 3 Pages,

I'm trying to view the map on that panel, but when I launch the app it crashes and sends an Exception, my code is:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")                                ' Load Main Layout

    If FirstTime Then
        GPS1.Initialize("GPS")                                ' Initialize GPS
        lblLon.Initialize("")
        lblLat.Initialize("")
        lblSpeed.Initialize("")
        lblSatellites.Initialize("")
    End If
    TabHost1.AddTab("Principal", "Page1")                     ' Adds Page1 on the first Tab
    TabHost1.AddTab("Navegador", "Page2")                     ' Adds Page2 on the second Tab
    TabHost1.AddTab("Facebook", "Page3")                     ' Adds Page3 on the third Tab
    If mFragment.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Google Play services not available.", True)
    Else
          mFragment.Initialize("Map", MapPanel)
    End If
End Sub

B4X:
Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

B4X:
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)
       CallSubDelayed(Me, "SetZoom")
    End If
End Sub

The exception:
android.content.res.Resources&NotFoundException: Unable to find resource ID %0xe


The first post has a reference to add the view panel with Addview, but I want to see the map inside the app with a Panel created and declared on Globals, I've tried to search the info on first panel Erel, but I'm a little confused,

Tnx
 

Noize

Member
Licensed User
Longtime User
Here's the file, the exception is not present today, lol :S , but it's not working again.
 

Attachments

  • app.zip
    12.5 KB · Views: 339
Last edited:

Noize

Member
Licensed User
Longtime User
Hi Erel, I've recoded the app building the tabs following the horizontal scroll view tutorial now, and it launches correctly without problems, don't know why it crashes with tabhost implementation but, now it's ok :).

My problem was that when I tried to show the map on a Panel created with the designer, oposite to create it whit Addview property , the program stoped with a java exception, but now it works and has the horizontal scroll, so.., it's ok for me!

Also, I've buyed the b4a digital book, I'm very excited with B4A.

Thanks for your help,

Cheers
 
Last edited:

Andy Barker

Member
Licensed User
Longtime User
OK - Answered my own question by asking it! Didn't copy the updated google-play-services.jar from the SDK folder to the B4A libs folder.



Firstly want to say "Thanks" for all the fantastic work you've done with B4A Erel, and the time and respect you give to your customers on here.

I've managed to find all the answers to any questions I've had through these forums, until now. Today I compiled an update to a GPS related app I started writing a few weeks ago, and I am getting an error that I don't understand the cause of. So I went back to a previous known version of code, and that now also results in the same error. I did update the Android SDK the other day, so I assume something has changed due to that.

The error is related to accessing the Google Maps code, when initializing the map. The error I get is:

B4X:
java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 5089000 but found 6111000.  You must have the following declaration within the <application> element:  <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
I have the following lines in the Manifest Editor:
B4X:
AddApplicationText(    
     <meta-data
      android:name="com.google.android.maps.v2.API_KEY"
      android:value="MY API KEY"/>
     <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />
       )
 
Last edited:
Status
Not open for further replies.
Top