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:

nidpea04

Member
Licensed User
Longtime User
My project Google map error with Run Release
-----------------------------------------
B4A version: 6.00 BETA #1
Parsing code. (0.00s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Generating R file. (0.33s)
Compiling generated Java code. (1.34s)
Convert byte code - optimized dex. (1.79s)
Packaging files. Error
 

nidpea04

Member
Licensed User
Longtime User
Thank Erel.
I'm remove v.6 beta. and install v5.8 ( windows 10 )
But there error.
'---------------------------------------------
B4A version: 5.80
Parsing code. (0.00s)
Compiling code. (0.08s)

ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code. (0.00s)
Generating R file. (0.25s)
Compiling generated Java code. (1.50s)
Convert byte code - optimized dex. (0.55s)
Packaging files. Error

Please advise me.
 

iCAB

Well-Known Member
Licensed User
Longtime User
After updating Google-play-services to Rev31, I commented out

B4X:
#AdditionalRes: C:\Android\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res,com.google.android.gms
and added the following

B4X:
#AdditionalJar: com.google.android.gms:play-services-maps

The code compiles no problem, but I am getting the following error now

B4X:
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4a.objects.collections.Map.Get(java.lang.Object)' on a null object reference
    at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady


I have modified the manifest to include
B4X:
'************ Google Play Services Base ************
AddApplicationText(
   <activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
  android:theme="@android:style/Theme.Translucent.NoTitleBar"
  android:exported="false"/>
    <meta-data
  android:name="com.google.android.gms.version"
  android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSy......"/>
)
'************ Google Play Services Base (end) ************

I also tried replacing maps.v2.API_KEY with geo-API as recommended in the first post but I still got the same error.

I have also tried following the instruction for downloading google-service.json. That also didn't make a difference.

One thing I have noticed, that after I updated google-play-services, I was unable to find a new version of google-play-services.jar installed on my PC. Is this file no longer needed

Thanks in advance
 
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
You need to load the map with the designer.
That is going to be an issue for the following reasons:
1. The application is huge and doesn't use the designer at all
2. All layouts/dimensions are automatically calculated during run time and auto adjusted to fit any screen size / resolution
3. I put a lot of effort in this so when I start doing my B4I conversion, there will be no dependency on the designer/GUI

Is there any other way? .. This was all working fine, until I updated the SDK (hoping to use Firebase )

BTW, I am not sure, about the designer thing, since I am facing the same issue with the sample project I uploaded for the Chopped Icons
( I believe this one is using the designer to load the map )

https://www.b4x.com/android/forum/threads/google-map-marker-icon-chopped.66781/#post-423450
 

Daniel-White

Active Member
Licensed User
Longtime User
That is going to be an issue for the following reasons:
1. The application is huge and doesn't use the designer at all
2. All layouts/dimensions are automatically calculated during run time and auto adjusted to fit any screen size / resolution
3. I put a lot of effort in this so when I start doing my B4I conversion, there will be no dependency on the designer/GUI

Is there any other way? .. This was all working fine, until I updated the SDK (hoping to use Firebase )

BTW, I am not sure, about the designer thing, since I am facing the same issue with the sample project I uploaded for the Chopped Icons
( I believe this one is using the designer to load the map )

https://www.b4x.com/android/forum/threads/google-map-marker-icon-chopped.66781/#post-423450

I come from B4A 5.8 and I was working all the google maps by code. in my particular case, I only added the fragment map to the layout and put it "invisible". that is it :) in my case.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
GoogleMaps v2 is implemented as a custom view and should be added with the designer.

2. All layouts/dimensions are automatically calculated during run time and auto adjusted to fit any screen size / resolution
It will be easier to do it with the designer.

3. I put a lot of effort in this so when I start doing my B4I conversion, there will be no dependency on the designer/GUI
It will be easier to port an application that uses the designer as the layout handling in iOS is different than Android.

Anyway you can create a layout with only the map and load it. It will not interfere with the other views.
 

KMatle

Expert
Licensed User
Longtime User
GoogleMaps v2 is implemented as a custom view and should be added with the designer.

Of course I ran into the same problem (adding it by code -> exception, designer -> ok). Could you please desribe where the exact difference is (adding by designer / by code) in that specific case?
 

luciano deri

Active Member
Licensed User
Longtime User
How can find multi point in map, having only addresses (street, number, city, country ...)
Thanks.
 
Status
Not open for further replies.
Top