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: 2,767
  • GoogleMaps.zip
    19.4 KB · Views: 2,915
Last edited:

javiers

Active Member
Licensed User
Error...


*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x2711
at android.content.res.Resources.getResourceName(Resources.java:2363)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1072)
at android.app.BackStackRecord.run(BackStackRecord.java:852)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
 

aeric

Expert
Licensed User
I have installed B4A 8.00. The default GoogleMaps library (v1.01) is outdated. The new one is (v2.00).
I suggest to replace the library in next update to avoid error.
 

aeric

Expert
Licensed User
Google Maps is an additional library which is NOT installed together with B4A.
Solution: Download the V2 Version, put it in your additional libs folder.
Ok. Sorry, I may have messed with the Library and Additional folder. Thanks.
 

MetalOS

Member
Licensed User
Hello,
I also encounter this error:

Maven artifact not found: com.google.android.gms/play-services-maps

I think I have reproduced the tutorial except for a part concerning Firebase. I downloaded the json file but I do not know how to declare it in my code for it to be taken into account. Can this lack of statement generate the error above? Thank you in advance for your help.
 

chardenal

Active Member
Licensed User
Hello,

It seems the file "GoogleMaps.zip" in attachment is the version 1.01 and not the version 2.00

So, the Mapfragment1" doesn't' appears in CustomView in the Designer...

Is it normal?

Thanks
 
Status
Not open for further replies.
Top