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:

BarryW

Active Member
Licensed User
Longtime User
I just got this Error suddenly...

java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 7095000 but found 7895000. 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" />


at com.google.android.gms.common.GooglePlayServicesUtil.zzJ(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
at anywheresoftware.b4a.objects.MapFragmentWrapper.IsGooglePlayServicesAvailable(MapFragmentWrapper.java:101)
at ne.itlection.test.main._load_map(main.java:2627)
at ne.itlection.test.main._btnmap_click(main.java:735)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)


at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
at android.view.View.performClick(View.java:4478)
at android.view.View$PerformClick.run(View.java:18698)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 7095000 but found 7895000. 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" />
 

warwound

Expert
Licensed User
Longtime User
@BarryW

I bet you have updated Google Play Services using the Android SDK Manager.
But you have not then copied the latest version of google-play-services.jar from the Android SDK folder to your b4a additional libraries folder.
The version of google-play-services.jar in your b4a additional libraries folder is not the latest version.
 

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to all and thanks to @Erel for this library :)
I'm using this library without problems but the only thing that I don't understand is how to set a predefined area around a Marker position.
In other words, I wish to set a radius and a predefined zoom around the marker (ex. 50 meters).

Actually the default zoom has an area that cover Europe, Africa and Atlantic Ocean.

Thanks in advance for the reply :)
 

eurojam

Well-Known Member
Licensed User
Longtime User
this is done with CameraUpdate newLatLngBounds (see https://developers.google.com/andro...gle.android.gms.maps.model.LatLngBounds, int))

in B4A you can do it like this:
B4X:
    Dim CameraUpdateFactory1 As CameraUpdateFactory
    Dim CameraUpdate1 As CameraUpdate 

     Dim llb As LatLngBounds
     Dim sw, ne As  LatLng
     sw.Initialize(47.9990077, 7.8421043)
     ne.Initialize(47.99900772, 7.84210432)
     llb.Initialize( sw,ne)
     CameraUpdate1=CameraUpdateFactory1.NewLatLngBounds(llb,10 )
     GoogleMapsExtras1.AnimateCamera(gmap, CameraUpdate1)
 

AusSteve

Member
Licensed User
Longtime User
I've done it this way


B4X:
   Dim MapCenter as Location
   Dim CP as CameraPosition
   Dim MapZoomLevel as int
   Dim GMap as GoogleMap
   Dim mFragment as MapFragment
   MapCenter.initialize
   mFragmetn.Initialize("Map", pnlMain)
   MapCenter.Latitude=-32.8
   MapCenter.Longitude=151.7
   MapZoomLevel=19 'pick an int that works
   GMap=mFragment.GetMap
   CP.Initialize(MapCenter.latitude,MapCenter.Longitude,MapZoomLevel)
   GMap.AnimateCamera(CP)
[/QUOTE]

I think I got it all..... the rest is as the tutorial and this isn't that different
 
Last edited:

mtechteam

Member
Licensed User
Longtime User
The syntax shown as

#AdditionalRes <google-play-services res folder>, com.google.android.gms

Should actually be

#AdditionalRes: <google-play-services res folder>, com.google.android.gms

The example has the colon, but I didn't notice that right away. Perhaps others may struggle with that.....
 

jamarquezw

Member
Licensed User
Longtime User
Hi:
I followed ther example, but the compiling process comes to the last line, takes several minuets and stops with timeout error.
B4A version: 5.50
Parsing code. (0.01s)
Compiling code. (0.15s)
Compiling layouts code. (0.00s)
Generating R file. (0.66s)
Compiling debugger engine code. (4.29s)
Compiling generated Java code. (6.41s)
Convert byte code - optimized dex.

After 5 minutes a timout error aprrears.
First i got an outofmemory error, and increased the MaxRamForDex=2000 parameter in b4xV5.ini file
I created all the keys, included the files in manifest, #AdditionalRes:D:\adt-bundle-windows-x86-20140702\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms, see the lib files in b4a.

What can be wrong ?

best regrads
 

jamarquezw

Member
Licensed User
Longtime User
Hi:

My total RAM 12 GB, used 47%
the compiling process creates 2 java thread, each one using 575M of ram
upload_2015-11-29_10-14-43.png

After 5 minutes compiling:
upload_2015-11-29_10-15-43.png


Thanks
 

jamarquezw

Member
Licensed User
Longtime User
Hi Erel

Yes, yes yes. It worked. That is why I chose B4A. I didnte restart the IDE after changind the ini. May thats the reason.
best regards
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All

I am trying to use google maps in my app.
I followed the instructions in the beginning of the post to configure google-play-services.
But I am still getting this line to return false

B4X:
if mFragment.IsGooglePlayServicesAvailable = False then

Here is what I have done so far:
1. Latest Java is installed
2. Google-Play-service is installed (Ver 28 )
3. I created a folder ( C:\Android\SourceCode\additional_library_files ) and copied google-play-services.jar & properties to the folder
4. I added the path in 3 to configure paths-.Additional Libraries
5. Google Maps and GoogleMapsExtras are checked in the project libraries
3. Key generated as per instructions at the beginning of the post and mainfest file updated accordingly


I also tried an Example project to narrow it down .. same issue
I am also including a copy of the manifest file in here just in case

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
    <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.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxQ"/>)
   
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(com.google.android.providers.gsf.permission.READ_GSERVICES)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.





Thanks in advance
 
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
That's what I suspected, however I didn't want to do that before I follow up with my next question.
So if I am releasing and app that uses google maps, what are my options to simplify the process for the end user?
Is there a way to auto invoke the installation of google play services when installing our app? I am sure many end users will appreciate this. Otherwise it might look like the app doesn't work and end the user will uninstall and never come back.

Please clarify
 
Last edited:
Status
Not open for further replies.
Top