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,255
  • GoogleMaps.zip
    19.4 KB · Views: 4,589
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