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:

netchicken

Active Member
Licensed User
Longtime User
Replacing the marker with a small 24x24 icon only shows about 1/4 of the icon, bottom left.

Is there a way to show the entire icon? with fill, stretch, center?
B4X:
activityimage= LoadBitmap(File.DirAssets,"still.png")
gmap.AddMarker3(newloc.Latitude, newloc.Longitude, activitytype,activityimage)
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi to christchurch,

last week i changed my tablet and on the new one with higher resolution, but same size i had the same effect. My quick and dirty trick was to change the background size of the bitmaps with paint.net. It worked for the moment, but its better to find out, what the source of this problem is.

Greetings
Reinhard
 

Mandrake

Member
Licensed User
Longtime User
Google maps for Android 2.2+

If you like I can upload the code and you can try to modify it as needed (I actually think that the code should be already available in this thread somewhere).

Please Erel if we can provide a tutorial or example of how to do it, I have completed my application but I only work for version 3 + need to work with the 2.2, 2.3 + please.
 

dealsmonkey

Active Member
Licensed User
Longtime User
Hi,

I am trying to get Maps v2 to work on a Nexus 7 tablet running 4.2.2 and am getting the following error :

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x23

I have checked and double checked settings, api key, libs etc and thoughts please ?
 

dealsmonkey

Active Member
Licensed User
Longtime User
Can you post the full error message (with the stack trace)?

No probs, here you go

B4X:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x23


   at android.content.res.Resources.getResourceName(Resources.java:1659)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
   at android.app.BackStackRecord.run(BackStackRecord.java:682)
   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
   at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5041)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)


   at dalvik.system.NativeStart.main(Native Method)
 

gadgetmonster

Active Member
Licensed User
Longtime User
Hi

On my daughters phone the line

B4X:
mFragment.IsGooglePlayServicesAvailable

returns true but

B4X:
mFragment.Initialize("Map", panelMap)

errors with

B4X:
mFragment.Initialize("Map", panelMap)
java.lang.NoSuchMethodError: android.app.Activity.getFragmentManager
   at anywheresoftware.b4a.objects.MapFragmentWrapper.Initialize(MapFragmentWrapper.java:115)
   at com.theappspod.dayjournal.locationgooglemaps._activity_resume(locationgooglemaps.java:375)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
   at com.theappspod.dayjournal.locationgooglemaps.afterFirstLayout(locationgooglemaps.java:95)
   at com.theappspod.dayjournal.locationgooglemaps.access$100(locationgooglemaps.java:16)
   at com.theappspod.dayjournal.locationgooglemaps$WaitForLayout.run(locationgooglemaps.java:74)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:138)
   at android.app.ActivityThread.main(ActivityThread.java:3701)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
   at dalvik.system.NativeStart.main(Native Method)

This works fine on my note 2 - any ideas?
 

daniel69

Member
Licensed User
Longtime User
Great library! Is there any way to make a screenshot programmatically? The agraham code provides a blank screenshot .... is it a problem with the GetField value?


Sub ScreeShot(fName As String)As String

Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Dim Out As OutputStream
Dim c1 As Int
Dim ret As Int

Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(Activity.Width, Activity.Height)
c.Initialize2(bmp)
Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"

Obj1.RunMethod4("draw", args, types)
Out = File.OpenOutput(File.DirDefaultExternal & "/" & globv.cCHARTS, Name, False)
bmp.WriteToStream(Out, 100, "JPEG")
Out.Close

End Sub
 

derez

Expert
Licensed User
Longtime User
I am running some modification on Erel's example, it runs ok on my Asus using android 4.0.3, but on my samsung s2 with 4.1.2 it doesn't show the map (actually it shows a white map), without any error.
Erel's example works on the samsung.
What can be the problem ?
 

derez

Expert
Licensed User
Longtime User
Hard to say. Have you checked the unfiltered logs?

The sub that works but show white map is :
B4X:
Sub Map_Ready
  Log("map ready")
 
  gmap = mFragment.GetMap
  If gmap.IsInitialized = False Then
      ToastMessageShow("Error initializing map.", True)
  Else
      gmap.MyLocationEnabled = True
      ml.Initialize( 32.1404164 ,  34.824858)
      If ml.IsInitialized Then
          Log("ml= " & ml.Latitude & "  " & ml.Longitude)
          gmap.AddMarker(ml.Latitude, ml.Longitude, "Me")
         
          Dim cp As CameraPosition
          cp.Initialize(ml.Latitude, ml.Longitude, 10) 'gmap.CameraPosition.Zoom)
          gmap.AnimateCamera(cp)
      Else
            ToastMessageShow("Error initializing Location", True)
      End If
  End If
  Log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
End Sub

The unfiltered log is:
map ready
ml= 32.1404164 34.824858
VFY: unable to resolve static field 602 (MapAttrs) in Lcom/google/android/gms/R$styleable;
VFY: replacing opcode 0x62 at 0x000d
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 

derez

Expert
Licensed User
Longtime User
I found the problem, it was with the sign key, like you warned in the first post...
 

warwound

Expert
Licensed User
Longtime User
GoogleMapsExtras has been updated to version 1.35

The new Snapshot method takes a snapshot of the current map.
The event SnapshotReady is raised when the snapshot has been created, the snapshot passed to your event handling Sub as a Bitmap.

B4X:
Sub Process_Globals
   '   i'll use a Process_Global to enable me to pass the snapshot Bitmap from this Activity to the SnapshotViewer Activity
   Dim LastSnapshot As Bitmap
End Sub

Sub Globals
  Dim MapFragment1 As MapFragment
  Dim GoogleMap1 As GoogleMap
   Dim GoogleMapsExtras1 As GoogleMapsExtras
  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 GoogleMapsExtras1_SnapshotReady(Bitmap1 As Bitmap)
   Log("GoogleMapsExtras1_SnapshotReady")
   LastSnapshot=Bitmap1
   StartActivity(SnapshotViewer)
End Sub

Sub MapFragment1_LongClick (Point As LatLng)
   '   a long click on the map will add a new Marker
   GoogleMap1.AddMarker(Point.Latitude, Point.Longitude, "("&Point.Latitude&", "&Point.Longitude&")")
End Sub

Sub MapFragment1_Ready
  Log("MapFragment1_Ready")
  GoogleMap1 = MapFragment1.GetMap
  If GoogleMap1.IsInitialized = False Then
  ToastMessageShow("Error initializing map.", True)
  Else
     GoogleMap1.AddMarker(52.75619, 0.3980, "Home Sweet Home")
    
  Dim CameraPosition1 As CameraPosition
  CameraPosition1.Initialize(52.75619, 0.3980, 6)
  GoogleMap1.AnimateCamera(CameraPosition1)
  End If
End Sub

Sub SnapshotButton_Click
   Log("SnapshotButton_Click")
   GoogleMapsExtras1.Snapshot(GoogleMap1, "GoogleMapsExtras1")
End Sub

This is the Main Activity, it shows a basic map with a single Marker.
A long click on the map adds a new Marker.

A click on the 'Take snapshot' button calls the new Snapshot method.
When the 'SnapshotReady' event is raised, the snapshot (a Bitmap) is saved as a ProcessGlobal and a new Actvitiy started.
The new Activity contains just an ImageView and displays the snapshot Bitmap centered in the ImageView.

IMPORTANT: In order to use the new Snapshot method you must ensure that your b4a additional libraries folder contains the latest version of google-play-services.jar.
Run the Android SDK Manager utility and check whether or not you have the latest version, i have tested with version 10 of the Google Play servcies library
.

Your attention is drawn to the notes from the GoogleMap API reference:

Note: Images of the map must not be transmitted to your servers, or otherwise used outside of the application. If you need to send a map to another application or user, send data that allows them to reconstruct the map for the new user instead of a snapshot.

Martin.
 

Attachments

  • Snapshot_example.zip
    9.1 KB · Views: 714
Status
Not open for further replies.
Top