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,242
  • GoogleMaps.zip
    19.4 KB · Views: 4,576
Last edited:

fbritop

Active Member
Licensed User
Longtime User
I have tried your example but it cannot compile:

Invalid resource directory name: C:\SDK\Android\tools\..\extras\google\google_play_services\libproject\google-play-services_lib\res/drawable-xxxhdpi

Part of the manifiest is:

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)

AddApplicationText(<meta-data
  android:name="com.google.android.maps.v2.API_KEY"
  android:value="AIzaSyB2_c2ooYp_I9Q2r-XXXXXXXXX"/>
   <meta-data android:name="com.google.android.gms.version"
   android:value="@integer/google_play_services_version" />
)

key has been replaced with my console value
 

rscheel

Well-Known Member
Licensed User
Longtime User
I have tried your example but it cannot compile:

Invalid resource directory name: C:\SDK\Android\tools\..\extras\google\google_play_services\libproject\google-play-services_lib\res/drawable-xxxhdpi

Part of the manifiest is:

B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)

AddApplicationText(<meta-data
  android:name="com.google.android.maps.v2.API_KEY"
  android:value="AIzaSyB2_c2ooYp_I9Q2r-XXXXXXXXX"/>
   <meta-data android:name="com.google.android.gms.version"
   android:value="@integer/google_play_services_version" />
)

key has been replaced with my console value

You need to place your api key.

B4X:
android:value="Here's your api key"
 

fbritop

Active Member
Licensed User
Longtime User
Where it says:
B4X:
android:value="AIzaSyB2_c2ooYp_I9Q2r-XXXXXXXXX"/>

I already have put the correct API key from the console. I just print out with XXXXX no to make it public to the forum
 

Daniel-White

Active Member
Licensed User
Longtime User
Sorry, Perhaps I will do a stupid question, what is the improvement, or difference with the old tutorial.?
 

ricardotm1968

Member
Licensed User
Longtime User
Hi Erel,

I'm trying to follow your example, follow the steps were as follows:
1. android SDK-Update
2. Copy google-play-services and android-support-v4 in additional libraries folder.
3. Register my project in google and got the API key for android
4. Copy the text to the manifest, replacing key API
5. Copy the attributes
6. Copy all code in an activity.
When compiling displays the MapFragment but can not find the map
What will be the error?
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I have changed the mapfragment object as this tutorial.
I'm using the GoogleMapsExtras library (AnimateToBounds) and I've noticed the following problem:
When I'm in debug mode, it works normally.
In release mode it does nothing. I can see the map but no movement!
Is there any suggestion?

Thank you in advance!
 

Anser

Well-Known Member
Licensed User
Longtime User
My app works fine on GoogleMaps Ver 1.01 but error out when I use ver 2.00, (I rechecked and confirmed that if I go back to ver 1.01, then it is working fine)

I was using the Google Maps Ver 1.01 as per the old tutorial
Today I changed to the new version of GoogleMaps ie ver 2.00

The error is that the Map object is accessed before it is initialized, but the same code is working fine if I use ver 1.01

Is there a minimum version for the Google Play Services to be used along with this version of GoogleMaps ?

As of now I am using Google Play Services ver 28, I know that ver 29 is available, but haven't upgraded it as I read discussions regarding few bugs in Android Support Library and AppCompat etc. So I am waiting for a bug free version of Android Support Library. I believe that I can upgrade just the Google Play Service alone.

I already have the API Key, I hope that I don't have to use a new API Key to use GoogleMaps ver 2.00


Regards
Anser
 

MetalOS

Member
Licensed User
Longtime User
Hello,

I followed the tutorial but the Google map remains white and no tile is loaded. here's a screenshot to show you the problem.


157394Screenshot20160308020403.png
 

stanks

Active Member
Licensed User
Longtime User
in that Erel example from first post...how to remove marker and show another one on anther position?
 

fbritop

Active Member
Licensed User
Longtime User
Has someone has got this error and how to fix it?

B4X:
B4A version: 5.80
Parsing code.    (0.20s)
Running custom action.    (0.04s)
Compiling code.    (0.43s)
Compiling layouts code.    (0.00s)
Generating R file.    Error
invalid resource directory name: c:\sdk\android\extras\google\google_play_services\libproject\google-play-services_lib\res/drawable-xxxhdpi

I have double checked than "c:\sdk\android\extras\google\google_play_services\libproject\google-play-services_lib\res" exists
My manifiest has:
B4X:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>

Api key is correct

Additional Libraries folder contains: android-support-v4.jar; GoogleMaps.jar; GoogleMaps.xml; google-play-services.jar; google-play-services.jar.properties

Cannot seem to find where is the error. Any help?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Cannot seem to find where is the error. Any help?
Do you see this folder <sdk path>\extras\google\google_play_services\libproject\google-play-services_lib\res\drawable-xxxhdpi ?

why mapfragment cannot set position(left,top,width,height) with code
The correct way to set its layout is with the designer. MapFragment is not a view. If you need to move it at runtime then put it in a panel and move the panel.
 

MODERN TALKING

Active Member
Licensed User
Longtime User
Do you see this folder <sdk path>\extras\google\google_play_services\libproject\google-play-services_lib\res\drawable-xxxhdpi ?

I too don't have this Folder Erel, even after installing Google Play Services with AVD Manager.

This ERROR appears: AndroidManifest.xml:33: error: Error: No resource found that matches the given name (at 'value' with value '@Integer/google_play_services_version').

אנא עזור
 
Status
Not open for further replies.
Top