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

- 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"/>
)


- 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 Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private MapFragment1 As MapFragment
    Private gmap As GoogleMap
    Private rp As RuntimePermissions
End Sub

Public Sub Initialize
    
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Wait For MapFragment1_Ready
    gmap = MapFragment1.GetMap
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result Or rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) Then
        gmap.MyLocationEnabled = True
    Else
        Log("No permission!")
    End If
End Sub


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

  • GoogleMaps.zip
    19.4 KB · Views: 5,267
  • MapExample.zip
    14.4 KB · Views: 115
Last edited:

aeric

Expert
Licensed User
Longtime 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
Longtime 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
Longtime 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.
 

Arnaud

Active Member
Licensed User
Longtime 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