Android Tutorial (old) Google Maps Android v2 tutorial

Status
Not open for further replies.
If you are using B4A v5.80+ then please follow this tutorial: https://www.b4x.com/android/forum/threads/google-maps.63930/#post-404386

GoogleMaps library requires v2.50 or above.

GoogleMaps library allows you to add Google maps to your application. This library requires Android 3+ and will only work on devices with Google Play service.

This tutorial will cover the configuration steps required for showing a map.

1. Download Google Play services - From the IDE choose Run AVD Manager and then choose Tools - SDK Manager. Select Google Play services under the Extras node and install it:



2. Copy google-play-services.jar to the libraries folder - This file is available under:
C:\<android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\libs (ignore the extra space that the forum script insists on adding)
You should copy it to the libraries folder.

2.5. Download the attached library, unzip it and copy to the libraries folder.

3. Find the key signature - Your application must be signed with a private key other than the debug key. After you select a new or existing key file (Tools - Private Sign Key) you should reopen the private key dialog. The signature information will be displayed (increase the dialog size as needed).
The value after SHA1 is required for the next step:



4. Create an API project - Follow these steps and create an API project.
You should follow the steps under "Creating an API Project" and "Obtaining an API key".

Tips:
- Make sure to select "Google Maps Android API v2" in the services list and not one of the other similar services.
- Under "Simple API Access" you should select "Key for Android apps (with certificates".

5. Add the following code to the manifest editor:
B4X:
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="AIzaSyCzspmxxxxxxxxxxxxx"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
    />)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
You should replace the value after android:value with the key you received in the previous step.

6. Add an #AdditionalRes attribute to the main activity:

You should add a reference to Google play resources by adding the following line:
B4X:
#AdditionalRes: <google-play-services res folder>, com.google.android.gms
For example:

#AdditionalRes: C:\android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

Run the following code:
B4X:
'Activity module
Sub Process_Globals

End Sub

Sub Globals
   Dim mFragment As MapFragment
   Dim gmap As GoogleMap
   Dim MapPanel As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   MapPanel.Initialize("")
   Activity.AddView(MapPanel, 0, 0, 100%x, 100%y)
   If mFragment.IsGooglePlayServicesAvailable = False Then
      ToastMessageShow("Google Play services not available.", True)
   Else
      mFragment.Initialize("Map", MapPanel)
   End If
End Sub
Sub Map_Ready
   Log("map ready")
   gmap = mFragment.GetMap
   If gmap.IsInitialized = False Then
      ToastMessageShow("Error initializing map.", True)
   Else
      gmap.AddMarker(36, 15, "Hello!!!")
      Dim cp As CameraPosition
      cp.Initialize(36, 15, gmap.CameraPosition.Zoom)
      gmap.AnimateCamera(cp)
   End If
End Sub

You should see:



If you see a "white map" then there is most probably a mismatch between the: package name, sign key and the API key (from the manifest editor).

Google documentation: https://developers.google.com/maps/documentation/android/intro
Note that there is a required attribution which you must include in your app (see above link). You can get the string by calling MapFragment.GetOpenSourceSoftwareLicenseInfo.

V1.01: Fixes a bug in AddMarker2.
 

Attachments

  • GoogleMaps.zip
    17.8 KB · Views: 11,589
Last edited:

scrat

Active Member
Licensed User
Longtime User
MarkerOptions is used only in the creation of a Marker.
Arf google.... it's a pity

Can you modify your code so that any changes to the Map and other objects occur only in the UI thread?
Yes, now I parse all markers in a thread, put in a list only those who have changed visibility.
At the end of the thread I use Marker.visible on the elements of the list

The map scroll smoothly whith 3000 markers (just a few is visible at the same time)

Thanks for your reply , for this library and especially for CustomTileProvider !
 

warwound

Expert
Licensed User
Longtime User
Thanks for your reply , for this library and especially for CustomTileProvider !

Watch out...!

The CustomTileProvider has a pretty bad bug, it's not thread safe and is likely to throw occasional exceptions such as:

android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.

I checked the official documentation and noticed i'd missed an important note:

Calls to methods in this interface might be made from multiple threads so implementations of this interface must be threadsafe.

I've just made an update and am waiting for another forum member to give it a thorough test - if you'd like to give it a test too then let me know.

Martin.
 

scrat

Active Member
Licensed User
Longtime User
thank you Martin

I want to thoroughly test this update.
I have noticed 2 problems :
1) In zoom >13, the map background become black.
2) when the application returns from background the event CameraChange is not fired

I do not know if these problems are related to CustomTileProvider
 

warwound

Expert
Licensed User
Longtime User
First thing i'd suggest is to ensure that you have the latest version of the Google Play Services library.
It's been updated this month: https://developers.google.com/maps/documentation/android/releases
Look at both the 'Features' and 'Resolved Issues' - you might have an old version of Google Play Services which is buggy.

I've updated GoogleMapsExtras today so that many of these new features can be used in b4a.
Just need to implement the new GoogleMap setPadding method and it'll be ready.

You can download the updated CustomTileProvider and (not yet completely updated) GoogleMapsExtras from HERE.

CustomTileProvider no longer raises a 'GetTile' event.
It's Initialize method signature has changed too:

Initialize (ClassInstance As Object, SubName As String)

Here's an explanation of the ClassInstance parameter:
(That's copied from agraham's Threading library documentation - his library has been the basis of this update).

So instead of creating a sub to listen for the GetTile event you create a Sub that does exactly the same (creates and returns a Tile) and you pass this Sub name to the new Initialize method.

There's a demo b4a project in the above download - it should be easy to follow.

Martin.
 

scrat

Active Member
Licensed User
Longtime User
I downloaded the library, thank you
I test tomorrow.
I have the latest version of the Googleplay and I use the news functions setRotation and setFlat with the reflection livrary.
 

RichardHirst

Member
Licensed User
Longtime User
HI Martin.

Just noticed you are updating the excellent map library.. On the new features will you be adding the "setRotation" to a marker or custom bitmap marker?

Thanks
Richard

oh I noticed now this is for the whole map, not the marker....
 

scrat

Active Member
Licensed User
Longtime User
Martin,

I tested your 2 library
Everything seems to work perfectly.
setflat is ok
setRotation is ok
I have no problem now when the application returns from the background

It would be useful to add the setPosition function in MarkerExtras
I use reflection for this function

The black background problem in zoom> 13 still exists.
I can reproduce this with your example program.
Probably a bug in GooglePlay or in the drivers of my device

Thanks again
 

warwound

Expert
Licensed User
Longtime User
@scrat

My other CustomTileProvider tester has also told me that the update is working well so i'll remove the old version and release this update.

The b4a GoogleMaps library Marker object has a read/write property named Position - this is the Marker setPosition method you are looking for i think?
Take a look: http://www.b4x.com/android/help/googlemaps.html#marker_position

Does this black background only occur when you use the CustomTileProvider?
Do you mean that instead of the usual light colored background you see black but this is only while you are waiting for tiles to load?

@RichardHirst

The updated GoogleMapsExtras supports the new Marker setFlat, setInfoWindowAnchor and setRotation methods.
These are methods that apply to a Marker not the GoogleMap.

If you look at the official documentation release notes i think (hope!) you'll see that just about every feature of the android/java GoogleMaps library is now available in b4a.

Now i have to decide whether to incorporate the CustomTileProvider into GoogleMapsExtras or leave it as a separate library.
I think it makes sense to incorporate it into GoogleMapsExtras - if anyone following this thread has objections then let me know.


So i'll wait for anyone to comment on whether or not to incorporate CustomTileProvider into GoogleMapsExtras and then release the new GoogleMapsExtras.

Martin.
 

scrat

Active Member
Licensed User
Longtime User
The b4a GoogleMaps library Marker object has a read/write property named Position
You're right
I complicate my life for nothing!

Does this black background only occur when you use the CustomTileProvider?
Yes see the 2 screenshoots:

With customtileprovider



Whithout


it happens only from the zoom 14 and above even if the tiles are cached
 

warwound

Expert
Licensed User
Longtime User
But does this black background disappear as tiles are loaded?
Or is this an area on the map where your CustomTileProvider has no tiles and has returned NO_TILE for this area?

What device and android version are you using?

I was testing the new CustomTileProvider on an old HTC Desire S earlier.
The HTC is running a custom Jelly Bean ROM and showed no black background for any areas at any zoom levels.
The CustomTileProvider returns tiles for a small area, outside of that area it returns the NO_TILE constant.

Martin.
 

scrat

Active Member
Licensed User
Longtime User
Yes, black background disappears when tiles are loaded.
if there are no tile (NO_TILE returned) the location of missing tile is black.
If tiles are presents and cached, there just has a little black flash

Device : Huawei ascend Mate
Android JB api 16
 

warwound

Expert
Licensed User
Longtime User
I could test on another phone tonight.

OK and post with your results and the other device model and android version.

If you want me to test your project on a Galaxy S3 and Tab2 then let me know.

Martin.
 

jota1010

Member
Licensed User
Longtime User
the map is loaded and displays the image, but not drawn the benchmark.
agrege interruption point after the line Log ("map ready")
and never step over there
 

scrat

Active Member
Licensed User
Longtime User
I just tested on an Acer E2. (Quad core MT6589, API 17) (CPU K3V2 on Huawei ascend mate )
Same problem with black background (Zoom > 13)


Strange
 

RichardHirst

Member
Licensed User
Longtime User
Hi Martin.

Have you release the new GoogleMapsExtras, I have 1.36.

The new Marker setFlat, setInfoWindowAnchor and setRotation methods are not listed in this library.

Thanks

Richard
 

scrat

Active Member
Licensed User
Longtime User
Martin,

So i'll wait for anyone to comment on whether or not to incorporate CustomTileProvider into GoogleMapsExtras and then release the new GoogleMapsExtras.

Yes, I think that it is logical to include this feature in GME.
This is just my opinion, you're the Boss !
 

warwound

Expert
Licensed User
Longtime User
Hi Martin.

Have you release the new GoogleMapsExtras, I have 1.36.

The new Marker setFlat, setInfoWindowAnchor and setRotation methods are not listed in this library.

Thanks

Richard

These new methods are part of the not yet uploaded version 1.40 update.
I'm gonna wait and see what can be done regarding this issue before i upload it.
Version 1.40 implements the OnMyLocationButtonClickListener interface, and i can't test this until the issue is resolved.

Martin.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…