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,185
  • MapExample.zip
    14.4 KB · Views: 31
Last edited:

Aviles

Member
Licensed User
Longtime User
Hello.
I load the map and all Ok, but if I change to another app, when I return to my app the map is white, the google logo load, and the buttons for zoom loads but the map is white.

now my second problem is that the map looks rare when I return to my app and call to *.getMap in Activity_resume

1.png
2.png


In the first pic is when I start my app, the second is when I change to another app and return to my app
Note: the map is not rotated

can help me?
Thanks
 

GaryK4

Member
Licensed User
Longtime User
I read a lot and tried a lot, but I just cannot get maps to work for me. All I get is a white screen. The task manager shows "Finding Location" every 1-2 seconds. The Google maps app displays fine. There was an new example app to test the white screen works.
Is there an example project for the new example?

"I've uploaded a new apk: www.b4x.com/android/files/googlemap.apk"
 

ykucuk

Well-Known Member
Licensed User
Longtime User
Hello,
is there possible listen Snippet click event ?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Hi, where can i download the google-services.json on the external link mentioned
from the firebase console where you setup your app. After saving the new app you get a download of the json.

If i remember correctly (i setup a google maps app for a customer a few days before) you need to get the google api key from the google console. No need to use the json in your project.

If you are using firebase components in your app then you need to setup it in fb console correctly and get the json from there. You need to place the json in your apps root project folder too.
But only for google-maps you dont need to have the json

See https://www.b4x.com/android/forum/threads/google-maps.63930/ (Post #1)

Please note the comments about b4a 6.
 

cas6678

Active Member
Licensed User
Longtime User
You must add it with the designer.

Ok, I understand that I must add MapFragment from the designer, ok.

and I can activate the visible property (true or false) in the designer, ok.

But how can I do true / false this property in the code to turn off or turn on the MapFragment ???

Is possible addview (MapFragment) , for example, in a panel after create with designer ???
 

cas6678

Active Member
Licensed User
Longtime User
thanks @Erel

Now I can modify the panel (show/hide), but measure (Width & Height) of MapFragment are the same of the designer. Any way to change measure in % por all device?
 

mkvidyashankar

Active Member
Licensed User
Longtime User
Im getting this errror, where i am missing the files?

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 348)
java.lang.NoClassDefFoundError: com.google.android.gms.maps.MapFragment
at anywheresoftware.b4a.objects.MapFragmentWrapper.Initialize(MapFragmentWrapper.java:120)
at anywheresoftware.b4a.objects.MapFragmentWrapper.DesignerCreateView(MapFragmentWrapper.java:105)
at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:70)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:158)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at com.pci.survey.main._activity_create(main.java:348)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at com.pci.survey.main.afterFirstLayout(main.java:102)
at com.pci.survey.main.access$000(main.java:17)
at com.pci.survey.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
 

scrat

Active Member
Licensed User
Longtime User
Hello,

Before the latest googleplay update I used the old googlemap library.
Now getMap() is replaced by getMapAsync() and the old library is unusable.
With this library I can't resize dynamically by code the mapfragment.
With designer I put the mapfragment in a panel.
I can move the panel and the mapframent but I can only resize the panel, the mapfragment have always the same size.
In my app the mapfragment size is not fixed. How can I anchor mapfragment and panel

Thanks
 
Status
Not open for further replies.
Top