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

imbault

Well-Known Member
Licensed User
Longtime User
Erel,
after download your library, and trying your sample, I got :

Compiling generated Java code. Error
B4A line: 30
gmap.AnimateCamera(cp)
javac 1.7.0_09
src\b4a\example\main.java:294: error: package com.google.android.gms.maps.model does not exist
mostCurrent._gmap.AnimateCamera((com.google.android.gms.maps.model.CameraPosition)(_cp.getObject()));
 

JTmartins

Active Member
Licensed User
Longtime User
Find the key signature

Hi guys,

How can I find the key signature ?

As step "3. Find the key signature", does not work here as it is described in first post.

Created the signature. have the key file created.

Go again to Tools -> Private sign key

but nothing is displayed..fields come up as

Name : unknown
Organization Unknown
country : Unknown
password : (yes, here I can see the one I've used)

I browse for the key file..but nothing else is displayed.

V 2.02 here

Thanks
 

RichardHirst

Member
Licensed User
Longtime User
TileOverlay

Hi Erel.

Would it be possible to add TileOverlay to this libraary at some stage. Need to add a fire line scan image at a Lat/Lon.

Thanks and great work

Richard
 

msawada

Member
Licensed User
Longtime User
White screen problem with tutorial example

Great addition to B4A, I've been wanting it for a long time. I've tried a few times and can only achieve a white screen. I've generated private keys (tried two different) from Google API with the SHA1 private sign key from B4a and the ";package.name" which is my package name from Tools->Package Name in B4a. I've added these to the manifest editor using the code from Erel's tutorial. I've put the google jar and play jar files into the library folder and see the googlemaps library and it is loaded for the tutorial code. I have google maps on my devices. When I install and run the tutorial code I only get the zoom buttons and a white screen. Erel mentions that the white screen problem may be due to issue with some in-congruence of the package name SHA1 key or google key. However, I think mine are consistent unless I have the package name wrong and I am supposed to use a package name other than the one from tools->Package name. Any thoughts?
 

msawada

Member
Licensed User
Longtime User
Yes it works great Erel. Any idea what I am doing wrong? It must be an issue with my package name. Am I using the correct package name when I request my API key? I named my package using Tools->Package name in the menu and then used that name in the API Gmap 2.0 request.
 

msawada

Member
Licensed User
Longtime User
Thanks Erel and sorry. I have it working! It was my fault. I had chosen to active the Google Maps API v2 key rather than what you specifically said which is Google Maps Android API v2 - which makes complete sense. Again, thanks and my apologies.
 

wes58

Active Member
Licensed User
Longtime User
Hi Erel,
I have been playing with the new map object and have a few questions:
- How can the zoom level be changed (after initial initialization)?
- How can the boundaries be set?
In the google documentation (HTTP://developers.google.com/maps/documentation/android/views) there are methods CameraUpdateFactory that allow to change this and other map parameters. But how can we do it in B4A?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1.
B4X:
Dim cp As CameraPosition
Dim LL As LatLng = map.CameraPosition.Target
cp.Initialize(LL.Latitude, LL.Longitude, NewZoom)
map.AnimateCamera(cp)
2. You can use Reflection to set the zoom and position based on the boundaries:
B4X:
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)
      CallSubDelayed(Me, "SetZoom")
    End If
End Sub

Sub SetZoom
   Dim sw, ne As LatLng
   sw.Initialize(10, 10)
   ne.Initialize(35, 35)
   ZoomToBoundaries(gmap, sw, ne, 10dip)
End Sub

'SouthWest -> NorthEast - the visible box
'Padding - Padding from the map edges
Sub ZoomToBoundaries (gm As GoogleMap, SouthWest As LatLng, NorthEast As LatLng, Padding As Int)
   Dim r As Reflector
   r.Target = r.CreateObject2("com.google.android.gms.maps.model.LatLngBounds", Array As Object(SouthWest, NorthEast), _
      Array As String("com.google.android.gms.maps.model.LatLng", "com.google.android.gms.maps.model.LatLng"))
   Dim cameraUpdate As Object = r.RunStaticMethod("com.google.android.gms.maps.CameraUpdateFactory", "newLatLngBounds", _
      Array As Object(r.Target, Padding), Array As String("com.google.android.gms.maps.model.LatLngBounds", "java.lang.int"))
   r.Target = gm
   r.RunMethod4("moveCamera", Array As Object(cameraUpdate), Array As String("com.google.android.gms.maps.CameraUpdate"))
End Sub

Note that I use CallSubDelayed as the map size is still 0 in the Ready event and this causes the zoom method to fail (see this link for more information: https://developers.google.com/maps/...gle.android.gms.maps.model.LatLngBounds, int))

3. Most or all of the factory features are already available with CameraPosition.
 

wes58

Active Member
Licensed User
Longtime User
1.
B4X:
Dim cp As CameraPosition
Dim LL As LatLng = map.CameraPosition.Target
cp.Initialize(LL.Latitude, LL.Longitude, NewZoom)
map.AnimateCamera(cp)
I thought that once CameraPosition is initialised, the better method would be to use CameraUpdateFactory.zoomTo(float) method, which I could probably use using Reflection.
I am trying to convert existing application that used WebView to the google maps API so there are still a few things that I have to overcome (now some things seemed so easy to do in WebView using java script.)

Thanks for a quick response.
 

RichardHirst

Member
Licensed User
Longtime User
PolyLines

Hi.

Would you have a quick example to add a Polyline to the Map. Can not make it visible..?

Thanks

Richard
 

wes58

Active Member
Licensed User
Longtime User
In my application I want to show the map when the user presses the button. When the map is not needed the user presses the button again and the map should be removed. The only way I can remove (or really only hide) the map is to make the panel the map is on not visible. But this way the map is still active and is using GPS and data.

Is there any way to remove (destroy) the map object when not needed?
 
Status
Not open for further replies.
Top