Android Tutorial Google Maps

Status
Not open for further replies.
upload_2016-2-24_12-2-46.png



Installation instructions:

- Register your app in Google developer console: https://console.developers.google.com
You need to enable Google Maps Android API.
Then click on Credentials -> Create Credentials -> API Key -> Android Key.
You should get a key that looks like: AIzaSyCmFHamGE0O0BvxxxxxxxxXbTCSrjFVg-Q

- Copy the attached library and put it in the additional libraries folder. Add a reference to the GoogleMaps library.

- Add this to the manifest editor (replace the value with your key):
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)

AddApplicationText(
<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="xxxxxxxxxxxxxx"/>
)

AddApplicationText(
<uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
)


Don't add #AdditionalRes or #ExcludeClasses.

- Set minSdkVersion to 14 (Android 4).

- Add a MapFragment with the visual designer. It will appear under the CustomView menu. If you don't see it then make sure that the GoogleMaps library is selected.
You can change the properties from the designer.
Set the anchors to BOTH so the map will fill the activity:

SS-2016-02-24_12.13.18.png



Implement the Ready event to get a reference to the GoogleMap object.
Complete code:
B4X:
Sub Process_Globals
   Private rp As RuntimePermissions
End Sub

Sub Globals
   Private gmap As GoogleMap
   Private MapFragment1 As MapFragment
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Wait For MapFragment1_Ready
   gmap = MapFragment1.GetMap
   rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result Then
       gmap.MyLocationEnabled = True
   Else
       Log("No permission!")
   End If
End Sub

Notes


Updates

-
v2.50 - Required dependencies were added to the library.
My Location Enabled property was removed from the designer as it needs to be set after the permission is granted.
- v2.02 EXTERNAL_STORAGE permission removed. It is no longer required.
GetOpenSourceLicenseInfo returns an empty string.
Fixes an issue with the ready event being lost if the activity is paused before the map is ready.

Note that you will need to update the package name and api key in the attached example.
 

Attachments

  • GoogleMapsExample.zip
    8.4 KB · Views: 4,583
  • GoogleMaps.zip
    19.4 KB · Views: 4,942
Last edited:

Nostrildumbass

Member
Licensed User
Longtime User
Hi everyone, I just wanted to report a finding I made after a bit of troubleshooting. I had the same issue as jchal with the error: ERROR: resource directory 'C:\android\tools\..\extras\google\google_play_services\libproject\google-play-services_lib\res' does not exist.

I found out that my #AdditionalRes attribute (below) was looking at the folder google_play_services, and my folder was called google-play-services. I renamed the folder and everything is looking fine so far.

#AdditionalRes: C:\android\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
 

Croïd

Active Member
Licensed User
Longtime User
Hi,
I changed my application with this new way to insert Google maps.
Now, I have this error ANR !
What is it about ?
 

Attachments

  • ANR- Galaxy S6.txt
    130 KB · Views: 659

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is not related to GoogleMaps.

It is related the MySQL library that you use.
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)

Switch to RDC and everything will work properly.
 

Haris Hafeez

Active Member
Licensed User
Longtime User
You mention Don't add #AdditionalRes or #ExcludeClasses

I take it that you mean not to use additional resources related to Google maps? I ask this because I use AppCompat and other libraries that require additional resources. Hope it doesn't mean an end to this support in 6.
 

DonManfred

Expert
Licensed User
Longtime User

ivan.tellez

Active Member
Licensed User
Longtime User
Hi everyone, I followed the instructions by @Erel :

Instructions for B4A v6+ were added to the first post.

But, Im having a little problem with B4A 6.0 and Google Play Services v30

I updated B4A and the Android SDK, deleted the old google-play-services.jar from libs. Then I started a new project:

  • Follow the instructions on Integrating Firebase Services
  • Add Google Play Services Base snippet from the link above to the manifest editor.
  • Add this snippet to the manifest editor (replace the value with your key):

B4X:
AddApplicationText(
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSxxxxxxxxx "/>
)

  • Add this line:
B4X:
#AdditionalJar: com.google.android.gms:play-services-maps

But, got this error:

B4X:
B4A version: 6.00
Parsing code.    (0.00s)
Compiling code.    (0.04s)
Compiling layouts code.    (0.01s)
Organizing libraries.    (0.00s)
Generating R file.    (0.23s)
Compiling debugger engine code.    (0.64s)
Compiling generated Java code.    (0.98s)
Convert byte code - optimized dex.    (1.66s)
Packaging files.    (0.98s)
Copying libraries resources    Error
No se pudo encontrar el archivo 'C:\Program Files (x86)\Anywhere Software\Basic4android\libraries\google-play-services.jar'.


Tested and working:
FireBase Ads
FireBase Remote Config
FireBase Analytics & crash reports
FireBase Notifications / Push messages

Do I have to use an older version of google-play-services.jar? Any idea on how to work with Google Play Services v30?

Thanks
 
Last edited:

ivan.tellez

Active Member
Licensed User
Longtime User
Thank you. I still don't understand why the old solution can not be maintained

You have the answer.

The old solution still sometimes works but is unstable.


I also worked with VB6, I actually mantained a couple of systems working fine along windows 98, 2000, Xp, Vista and 7, just dealing winth some UI tweaks in XP, then some security managment on Vista. Well mantained code in Vb6 Worked fine for almost 15 years.

As for Android, you have to leave the windows thinking. Nowadays, XP still has more than 20% of the windows marketshare, but, Android prior to 4.1 has les than 3% of android marketshare. So, why bother to mantain old code if nobody is going to use it?

I Upgrade all the code in my Apps to leave behind those old versions this january.

You should get the new B4A 6.0, update your SDK and put your code up to date.

If you have more questions, please estar another thread, this is for Google Maps. ;)
 

RichardN

Well-Known Member
Licensed User
Longtime User
Slightly confusing.....

The example manifest for Version 6 at the top of this thread appears to be missing the following couple of lines, it will not compile without:
B4X:
AddApplicationText(
<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="AIzaSxxxxxxxxx "/>

<meta-data android:name="com.google.android.gms.version"              <<<<<<<<<
   android:value="@integer/google_play_services_version" />              <<<<<<<<<
)
 

Daniel-White

Active Member
Licensed User
Longtime User
I ha
The following is the error log

** Activity (homepage) Pause, UserClosed = false **
** Activity (officelocations) Create, isFirst = true **
** Activity (officelocations) Resume **
java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:196)
at com.google.android.gms.maps.MapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.maps.internal.v$a$a.a:)com.google.android.gms.alldynamite:82)
at maps.ei.bu$6.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:181)
... 14 more


Regards
Anser

I have the same message but using B4A Ver 6.0 ?? when I try to run my old project from B4A 5.8 :mad: However the example of the post 1 work fine.:D
 
Last edited:

Daniel-White

Active Member
Licensed User
Longtime User
Slightly confusing.....

The example manifest for Version 6 at the top of this thread appears to be missing the following couple of lines, it will not compile without:
B4X:
AddApplicationText(
<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="AIzaSxxxxxxxxx "/>

<meta-data android:name="com.google.android.gms.version"              <<<<<<<<<
   android:value="@integer/google_play_services_version" />              <<<<<<<<<
)
I added it and now the map is working. in the example of post 1 :D
 

Daniel-White

Active Member
Licensed User
Longtime User
I added it and now the map is working. in the example of post 1 :D
Fixed, :D so simple really. :eek: only need add the custom MapFragment1 that is it. in my case I put it in visible = false in the Abstract designer. on purpose, and work fine. from B4A 5.80 to B4A 6.0 with out any pain , don't need change any code.

I keep using my API key of google map in the manifest with the Snippet B4A 6.0 manifest code. I import my project to firebase with the wizard of Firebase console,
I added the json file to project, I don't know if I really need it there.

using google map lib ver 2.0 and GoogleMapExtras too
and the play-service.jar empty gave us from Erel in this thread. to share lib folder .

and added in the manifiest.
AddApplicationText(
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSxxxxxxxxx "/>

<meta-data android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version" />
)

The google map is now very nice. :)
 
Status
Not open for further replies.

Similar Threads

Top