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:

Powie

Member
Licensed User
Longtime User
Hello, today I started to upgrade all libraries of our MADB project. I'm in problems in upgrading also the GoogleMaps libraries.... I think configuration of libraries, manifest and so on is OK, the map is displayed...

The issue for me ist that ( based on the tutorial code) MapFragment1_Ready() isn't triggered.
So I tried this also on the way to make a clean app with only the code from post1, struggling also with the same issue.
Triggered is Map_CameraChange...

Do I miss anything?
 

Powie

Member
Licensed User
Longtime User
Yes, I can confirm this. mapready was ignored, because after starting the activity I show a logo screen.

If I disable the intro and the activity map_ready works as designed.
 

ldb68

Member
Licensed User
Longtime User
How I can get from the map the current direction of the user (compass)?
I need the direction shown by the compass in the map without having to use the sensors directly (that gives me very mixed results).
thanks
 

Yayou49

Active Member
Licensed User
V5.80
Hi everybody,

Even after a long read of all requests of this thread, I didn't found a solution to my problem.
I've installed Google Play service Rev 39 in my SDK directory:

- No way to find google-play-services.jar in any of my drives
- you said to add a res like "<android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms"
but if I look into my directory "<android sdk>\extras\google\google_play_services\" I have only 2 sub directories: (docs & samples)

What's wrong ???....

Thanks in advance.
 

MarcoRome

Expert
Licensed User
Longtime User
V5.80
Hi everybody,

Even after a long read of all requests of this thread, I didn't found a solution to my problem.
I've installed Google Play service Rev 39 in my SDK directory:

- No way to find google-play-services.jar in any of my drives
- you said to add a res like "<android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms"
but if I look into my directory "<android sdk>\extras\google\google_play_services\" I have only 2 sub directories: (docs & samples)

What's wrong ???....

Thanks in advance.

Look THIS
My suggestion "Is time to switch Rel. B4A 6.80"
 

Yayou49

Active Member
Licensed User
Nice shot Marco !!!
Seems to work, in debug mode, it's compliling .... until "Installing file to device", I get this error:

XXXXX_RAPID_DEBUG.apk: 1 file pushed. 8.7 MB/s (2777756 bytes in 0.304s)
pkg: /data/local/tmp/XXXXX_RAPID_DEBUG.apk
Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]

Here is my manifest. If I remove the 2 first addManifestText, no more compiling error:

AddManifestText(<permission
android:name="$PACKAGE$.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>)
AddApplicationText(<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyA0kIYq2_tvkxxxxxxxxxxxxxxx-0"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version" />)


AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="17"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
AddManifestText(<uses-feature android:name="android.hardware.telephony" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)
'AddPermission("android.permission.ACCESS_COARSE_LOCATION")
AddManifestText(<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />)
AddPermission("android.permission.INTERNET")
'AddPermission("android.permission.ACCESS_FINE_LOCATION")
'AddPermission("android.permission.WAKE_LOCK")
'AddPermission("android.permission.DEVICE_POWER")
'AddPermission("android.permission.ACCESS_COARSE_UPDATES")
AddPermission("android.permission.READ_PHONE_STATE")
'AddPermission("android.permission.VIBRATE")
AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")
AddPermission("android.hardware.camera")
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")


Any idea ?
 

evbedp

Member
Licensed User
Longtime User
Dear Erel,

could you help me to solve this one?.. i got this error when i call this code .getMap()

i'm using B4A 6.80

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.NoSuchMethodError: No virtual method getMap()Lcom/google/android/gms/maps/GoogleMap; in class Lanywheresoftware/b4a/objects/MapFragmentWrapper$MyMapFragment; or its super classes (declaration of 'anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment' appears in /data/app/e.moto.workshop-1/base.apk)
    at anywheresoftware.b4a.objects.MapFragmentWrapper.GetMap(MapFragmentWrapper.java:132)
    at e.moto.workshop.main._gm_ready(main.java:943)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
    at anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment.onViewCreated(MapFragmentWrapper.java:191)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1072)
    at android.app.BackStackRecord.run(BackStackRecord.java:852)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1488)
    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:210)
    at android.app.ActivityThread.main(ActivityThread.java:5833)
    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:1113)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
 

evbedp

Member
Licensed User
Longtime User
Dear Erel,

could you help me to solve this one?.. i got this error when i call this code .getMap()

i'm using B4A 6.80

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.NoSuchMethodError: No virtual method getMap()Lcom/google/android/gms/maps/GoogleMap; in class Lanywheresoftware/b4a/objects/MapFragmentWrapper$MyMapFragment; or its super classes (declaration of 'anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment' appears in /data/app/e.moto.workshop-1/base.apk)
    at anywheresoftware.b4a.objects.MapFragmentWrapper.GetMap(MapFragmentWrapper.java:132)
    at e.moto.workshop.main._gm_ready(main.java:943)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:163)
    at anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment.onViewCreated(MapFragmentWrapper.java:191)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1072)
    at android.app.BackStackRecord.run(BackStackRecord.java:852)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1488)
    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:210)
    at android.app.ActivityThread.main(ActivityThread.java:5833)
    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:1113)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

i got this error when update google play service in Android SDK manajer to Rev 39. could you help me?

my project is work before i'm update google play service.
 

evbedp

Member
Licensed User
Longtime User
B4X:
PackageAdded: package:e.moto.workshop~i:** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x26
    at android.content.res.Resources.getResourceName(Resources.java:2442)
    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:1488)
    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:210)
    at android.app.ActivityThread.main(ActivityThread.java:5833)
    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:1113)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
 
Status
Not open for further replies.
Top