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:

SS-2012-12-18_18.28.04.png


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:

SS-2012-12-18_18.11.34.png


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:

SS-2012-12-18_18.25.14.png


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,426
Last edited:

bluedude

Well-Known Member
Licensed User
Longtime User
Hi Erel,

For a marker I need both Title and an Internal ID. When the marker is clicked I use the internal ID to lookup more external information. Can this be done?

Cheers,
 

warwound

Expert
Licensed User
Longtime User
Hi Erel,

For a marker I need both Title and an Internal ID. When the marker is clicked I use the internal ID to lookup more external information. Can this be done?

Cheers,

Ideally the Marker would have a Tag property but it doesn't.

Here's an idea...

Create a Map (not a GoogleMap!) where the Map keys are Marker objects and Map values are your 'internalIDs'.
When the MarkerClick event passes a Marker to your code you can then use that passed Marker to get the internalID from the Map.

Martin.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Is there also a way to delete a marker? Based on a location POI's change and I only want to show the relevant ones. For that I need to delete markers.

I know there is a remove option but not sure how to use that if I reload the whole marker list all the time. So sometimes based on location i have more POI's and sometimes less.
 

warwound

Expert
Licensed User
Longtime User
Look at the GoogleMapsExtras LatLngBounds object.

Could you construct a LatLngBounds object that is a rectangle of the area in which you want to display POIs?
Maybe take the user's current position and create a rectangle around that position.

Now the LatLngBounds object has a method:

Contains (LatLng1 As LatLng) As Boolean
Returns whether this LatLngBounds contains LatLng1.

Create a Marker for each of your POIs and add each Marker to the map, creating a List of your POI Markers while creating them.
Now iterate through that List checking if the LatLng of the POI is contained by the LatLngBounds.
If the POI is contained then make it visible if it's not currently visible, otherwise make it hidden (set it's Visible property to False).
You could iterate through this List each time the user location changes.

As long as you don't have so many POIs that adding them all to the map slows the map down it should work as you want.

You can create a Marker using one of the b4a GoogleMap AddMarker methods but this will force all Markers to be created as initially visible.

You could create your Markers using the GoogleMapsExtras AddMarker method, you'd create a MarkerOptions object for each Marker you want to create and this would allow you to create a Marker that is initially not visible.
So this might be the better choice.

Martin.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Warhound,

Thanks for the suggestion but that is no option, I cannot change my server api's right now to make a selection that way. Based on location there is always a different set of results.

Why isn't it possible to remove/delete a marker by an internal ID? It looks like I have to first add it right now and then set stuff like visibility etc.

Is there a way to clear all markers at once? Tried gmap.clear but does not do anything.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Done below code and it works:

'check for old markers
Dim markerObject As Marker
For Each keyObject As Object In markerMap.Keys
markerObject = keyObject
markerObject.Remove
Next
 

warwound

Expert
Licensed User
Longtime User
GoogleMapsExtras version 1.36 adds support for for both Indoor Maps and Traffic Layer.

This example shows a basic map with 2 ToggleButtons.
The ToggleButtons allow you to enable and disable Indoor Maps and Traffic Layer.

B4X:
Sub Process_Globals

End Sub

Sub Globals
  Dim GoogleMap1 As GoogleMap
   Dim GoogleMapsExtras1 As GoogleMapsExtras
  Dim MapFragment1 As MapFragment
  Dim MapPanel As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
  If MapFragment1.IsGooglePlayServicesAvailable = False Then
  ToastMessageShow("Google Play services not available.", True)
  Else
  MapFragment1.Initialize("MapFragment1", MapPanel)
  End If
End Sub

Sub MapFragment1_Ready
  Log("MapFragment1_Ready")
  GoogleMap1 = MapFragment1.GetMap
  If GoogleMap1.IsInitialized = False Then
  ToastMessageShow("Error initializing map.", True)
  Else
  Dim CameraPosition1 As CameraPosition
  CameraPosition1.Initialize(51.517900, -0.096614, 17)
  GoogleMap1.AnimateCamera(CameraPosition1)
  End If
End Sub

Sub IndoorToggleButton_CheckedChange(Checked As Boolean)
   GoogleMapsExtras1.SetIndoorEnabled(GoogleMap1, Checked)
End Sub

Sub TrafficToggleButton_CheckedChange(Checked As Boolean)
   GoogleMapsExtras1.SetTrafficEnabled(GoogleMap1, Checked)
End Sub

The map's initial position is set to (51.517900, -0.096614) at zoom level 17.
At this position and zoom level you'll see both Indoor Maps and Traffic Layer when enabled using the ToggleButtons.

Martin.
 

Attachments

  • Traffic_IndoorMaps.zip
    7.7 KB · Views: 677

Robby Mamotte

Member
Licensed User
Longtime User
Hi Erel
I also do not want to look stupid on this post and do not wish to repeat posts, but I have followed every instruction on this setup and cannot get my library to display GoogleMaps under libs. I have installed plenty of other library files and have had success with all, but with this google-play-services.jar library it doesnt show. I have closed and open basic4android after copying to Library or additional Library folder somehow it seems it is missing the xml file and when you say Android 3+ support is it the platform? if so mine is showing 4.2.2. I have gone through this entire post looking for solutions. Your assitance would be greatly appreciated Thank you (google extras is showing)
 

RichardHirst

Member
Licensed User
Longtime User
Hi.

I have tested the demo code on my Samsung phone running 4.1.2 but whenI install the same code onto the Samsung Tab 2 running 4.0
4 I get a white map...

Both have google map installed and running the lastest version.

Any fix please

Thanks

Richard
 

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi, I try to load de map from google but I just got white screen, but the demo application works fine. my question is:
arte there any bug with this lib when i'm using gps first?

regards,
Victor
 

victormedranop

Well-Known Member
Licensed User
Longtime User
Have you enabled the correct Google maps service?
Hi Erel, i'm having the same problem but I have google map install, i did all the step many times and i only have de white screen,
i really don't know where to check. in the idle i use the same package name b4a.locate use the sha1 and create my account an have my key.
is there another thing to check??

regards,

Victor
 

victormedranop

Well-Known Member
Licensed User
Longtime User
I finally made that working, it was an error in the code. thanks gad to log and logcolor.
This IDE is amaizing !!!!!
 

scrat

Active Member
Licensed User
Longtime User
Hello !
I have a problem with MarkerOptions.
I try to change the visibility of a marker with MarkerOptions
I can set the visibility of a marker before the creation of the marker but not after

B4X:
Dim Gm as GoogleMap
Dim Gme as GoogleMapsExtras
Dim Mo as MarkerOptions
Dim Ma as Marker
Mo.Initialize
Mo.visible(True) or False
......
Ma=Gme.AddMarker(Gm,Options)

This code works and the visibilty is set correctly.
When the marker is created I can not change its visibility with MarkerOptions.

B4X:
log(Mo.IsVisible) ===> True
Mo.visible(False)
log(Mo.IsVisible) ===> False

The visibility is not set to False and Marker is always visible

The same Operation with Ma.visible=false work, but not in a Thread

Any idea

Thanks
 

warwound

Expert
Licensed User
Longtime User
MarkerOptions is used only in the creation of a Marker.
Once you have created a Marker you'd use the b4a GoogleMaps library Marker Visible property to change the visibility:

B4X:
Ma.Visible=False

Oops you've tried that and it works but not in a Thread!
The answer is that you're not supposed to be making changes to such properties in a Thread.
Only the UI thread should make such changes.

Can you modify your code so that any changes to the Map and other objects occur only in the UI thread?

Martin.
 
Status
Not open for further replies.
Top