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:

evbedp

Member
Licensed 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)
 

evbedp

Member
Licensed User
i can't found MapFragment in designer, that's why i'm initializing in my code.
upload_2017-5-3_14-46-9.png

I got a different error than the one you got. You need to add MagFragment with the designer instead of initializing it in your code:

SS-2017-05-03_10.37.03.png
 

ThePuiu

Active Member
Licensed User
I followed all the steps in the tutorial (hope!), but when compiling, I get the error message:
B4A version: 6.31
Parsing code. (0.00s)
Compiling code. (0.06s)
Compiling layouts code. (0.00s)
Organizing libraries. Error
Maven artifact not found: com.google.android.gms/play-services-maps

I attached a small test project to show the error. Attached is also the SDK Manager configuration.
Pls, can anyone tell me what's wrong?
 

Attachments

  • installed.jpg
    installed.jpg
    119.4 KB · Views: 290
  • test.zip
    8.2 KB · Views: 323

ThePuiu

Active Member
Licensed User
I tried to add this code to the manifest editor, but the result is the same:
Maven artifact not found: com.google.android.gms/play-services-maps

Pls help!

B4X:
'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
  android:protectionLevel="signature" />)
AddApplicationText(
<receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  android:exported="false"/>
   <provider
  android:authorities="${applicationId}.firebaseinitprovider"
  android:name="com.google.firebase.provider.FirebaseInitProvider"
  android:exported="false"
  android:initOrder="100" />
    <receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  android:exported="false"/>
   <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
  android:exported="true"
  android:permission="com.google.android.c2dm.permission.SEND" >
  <intent-filter>
  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
  <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
  <category android:name="${applicationId}" />
  </intent-filter>
  </receiver>
    <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
  android:exported="false" />


  <service
  android:name="com.google.firebase.iid.FirebaseInstanceIdService"
  android:exported="true">
  <intent-filter android:priority="-500">
  <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
  </intent-filter>
  </service>
)
'************ Firebase Base (end) ************
 

ThePuiu

Active Member
Licensed User
I just want to put some markers on the map. I do not know for which of the above code lines is needed. Whether I put all the code from the "Always required when using Firebase:" section, or just left a part of it or deleted it from the manifest editor, the error message from the compilation was the same. If you think the problem comes from the manifest code, please make a copy / paste with the required piece of code. I think a functional example of the attached library was very good for us who are at the beginning of the road ...
I hope not bother you too much!
 

ThePuiu

Active Member
Licensed User
Sorry, but I do not see any difference from the code posted. Instead, I discovered a possible problem: i don't have that path on my computer:
<android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\res
There are only 2 subfolders in my google_play_services folder: docs and samples and there is no \ libproject \ google-play-services_lib \ res folder...
it is normal?
If I comment the line #AdditionalJar: com.google.android.gms:play-services-maps I'm not getting an error anymore, obviously the map does not work
 

ThePuiu

Active Member
Licensed User
The problem was generated by an incorrect path to the android.jar file! Now it's ok. I want to draw a line between the current position and a marker on the map. Line must move with the movement of the current position. How can this be done without leaving the previous lines on the map?
Thank you for your patience Manfred!

later edit:
Another problem for me is that I want the application to work in background with the tablet paused. When I get close to a marker to alert me. I have initialized the GPS in Starter but the application does not work in background. The GPS remains active (I think), but the sound signal is only received after the tablet comes out of the pause. Can I do what I want?
 
Last edited:

DonManfred

Expert
Licensed User
Image the situation. You get a new locationevent. Inside the event you get the actual position (before you change it) of the marker.
You now can draw a polyline from the old location to the new location. You then change the markers location to the new one to let the marker "move"...
But with creating a new polyline in each locationevent you can draw a line for the hole trip.

That`s all theory but it should work like this i guess. I never needed it.

BUT: You should create a NEW thread in the Questions forum for any new question you have.
 

shb777

Active Member
Licensed User
why can't you get it to work. and zip it up, so we can download it, and it will work right away?
 
Status
Not open for further replies.
Top