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

warwound

Expert
Licensed User
Longtime User
Martin,

OSMDroid library methodology has a number of developer-friendly overlays (MyLocationOverlay, PathOverlay, ScaleBarOverlay,MarkersBalloonOverlay, to name a few). Have you (or would you) considered adding this kind of functionality to the GoogleMaps library methodology?

One thing that the GoogleMaps library doesn't allow for is exactly what you are asking - custom overlays/custom map objects.
Google in their wisdom decided to make all GoogleMaps classes final - no way to extend them so no way to create your own custom objects.
You have the built in GoogleMaps objects and that's all that is available/possible.

Having said that, it's not a problem i think for your project.
  • MyLocationOverlay can be replaced with the GoogleMap MyLocationEnabled property and the MapUiSettings MyLocationButtonEnabled property.
  • GoogleMaps doesn't have overlay layers that contain a collection of overlays.
    If you want to display a Marker or Polyline you simply add it to the GoogleMap using the various AddMarker?? and AddPolyline methods.
    Markers automatically display a balloon when clicked.
  • It seems that there is no equivalent to the OSMDroid ScaleBarOverlay though.
    Even the official android Maps application has no default scale bar - i'm sure the official app used to have a 'Labs' page in it's settings where a scale bar could be enabled. But i see no Labs option in the latest version of the Maps app and no way to enable a scale bar.
    Strange that such a useful feature is not available in either the official app or the SDK library.
    And with no way to create custom map objects there is no way to create your own scale bar.

Martin.
 

ValDog

Active Member
Licensed User
Longtime User
The GoogleMap GetUiSettings method returns a MapUiSettings object which has the following methods and properties:

MapUiSettings
Methods:

  • IsInitialized As Boolean
Properties:
  • AllGesturesEnabled As Boolean [write only]
    Enables or disables all gestures.
  • CompassEnabled As Boolean
    Gets or sets whether the compass is enabled.
  • MyLocationButtonEnabled As Boolean
    Gets or sets whether the my-location button is enabled.
  • RotateGesturesEnabled As Boolean
    Gets or sets whether rotate gestures are enabled.
  • ScrollGesturesEnabled As Boolean
    Gets or sets whether scroll gestures are enabled.
  • TiltGesturesEnabled As Boolean
    Gets or sets whether tilt gestures are enabled.
  • ZoomControlsEnabled As Boolean
    Gets or sets whether zoom controls are enabled.
  • ZoomGesturesEnabled As Boolean
    Gets or sets whether zoom gestures are enabled.

So to enable display of the compass you'd do something like:

B4X:
GoogleMap1.GetUiSettings.CompassEnabled=True

The official documentation for the UiSettings class can be found here:
https://developers.google.com/maps/...erence/com/google/android/gms/maps/UiSettings.
Even though it's the java documentation it should still give you an idea of what each setting does.

Martin.


Martin,

I can't seem to get the compass to display - any suggestions?

Also, I presume there is not way to display satellite or terrain info without using a WebView?
 

warwound

Expert
Licensed User
Longtime User
The compass is enabled by default BUT only appears when the map is tilted or rotated.

So run your app, place two fingers side by side on the screen and drag down.
You should see the map tilt - viewed from an angle instead of overhead.
The compass will appear, a click on the compass restores the default overhead view and the compass will disappear when the map is viewed from overhead.

Place two fingers on the map and rotate the map - again the compass will appear, and again a click on the compass restores the default (not-rotated) view.

You can display other map types by setting the GoogleMap MapType property to one of the MAP_TYPE_???? constants.
For example:

B4X:
GoogleMap1.MapType=GoogleMap1.MAP_TYPE_TERRAIN

Martin.
 

ValDog

Active Member
Licensed User
Longtime User
Martin,

Excellent - thank you!!

Would you consider adding the PolylineOptions object so properties such as width and color could be manipulated?
 

warwound

Expert
Licensed User
Longtime User

ValDog

Active Member
Licensed User
Longtime User
Martin,

Sorry about that - I missed it. Must have had a senior moment...

Thank you again!
 

katrad

Member
Licensed User
Longtime User
I keep getting this error when I try to compile, any ideas?

B4X:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/R$attr;
    at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
    at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
    at com.android.dx.command.dexer.Main.processClass(Main.java:490)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
    at com.android.dx.command.dexer.Main.access$400(Main.java:67)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
    at

Thanks in advance.
 

warwound

Expert
Licensed User
Longtime User
I keep getting this error when I try to compile, any ideas?

B4X:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/R$attr;
    at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
    at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
    at com.android.dx.command.dexer.Main.processClass(Main.java:490)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
    at com.android.dx.command.dexer.Main.access$400(Main.java:67)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
    at

Thanks in advance.

Are you using Erel's updated instructions for version 3.20 and also GoogleMapsExtras?

Martin.
 

katrad

Member
Licensed User
Longtime User
Are you using Erel's updated instructions for version 3.20 and also GoogleMapsExtras?

Martin.

Yes, but when I checked the google maps extra's area, I had not copied the google-play-services (etc). res folder to my projects res folder once I did that and removed the new 'additionalRes' line for 3.2, it did compile.
 

warwound

Expert
Licensed User
Longtime User
@katrad

You original problem was caused by using the new #AdditionalRes attribute along with GoogleMapsExtras.

This post of mine explains how i updated GoogleMapsExtras so that it contained an R class that referenced all of the GoogleMaps 'android library' resources.
That post was made before the introduction of the #AdditionalRes attribute.

If you follow the instructions in post 1 of this thread and use the #AdditionalRes attribute (which requires the latest version of B4A) and also use GoogleMapsExtras then the R class that references these android library resources will be duplicated.

Once version 3.20 of B4A is officially released i shall update GoogleMapsExtras so that it doesn't contain this R class, instead developers will be expected to use the #AdditionalRes attribute.

For now a temporary fix is to do as you did - not to use the #AdditionalRes attrbiute if you also want to use GoogleMapsExtras.

Martin.
 

jjspike

Member
Licensed User
Longtime User
Hello,

would these libraries allow one to move a marker/icon live as an object moves? I would like to build a simple app that can track an RC multirotor via wireless telemetry. The receiver will connect to the Android device via USB/serial and update every 5 seconds. If this is possible, can someone kindly post the method to update the on-screen marker/icon (without the USB/serial code).

Thank you

JJ
 

jjspike

Member
Licensed User
Longtime User
Thank you Erel. Can you point me to an example of the command used to do this?

Thanks!

JJ
 

jjspike

Member
Licensed User
Longtime User
Hi warwound,

I tried using your library to take advantage of "Point" so I can update the position of my marker. Do you have an example of how I can use this to move the marker in real time?

Thanks

JJ
 

jjspike

Member
Licensed User
Longtime User
Thank you Erel. I did try this however there was an issue which was related to value that I was trying to assign m.Position. Sorry, I do not have the code with me at this time so I cannot post it. How should NewPosition be formatted/presented. I recall the value was suppose to be "LatLng". I do not know how to accomplish this: m.Position = NewPosAsLatLng


Thank you

JJ
 

ValDog

Active Member
Licensed User
Longtime User
If I were plotting changing locations on the map with an marker (icon) and displaying the path with a polyline, how would I go about (say from an Activity Menu) clearing the map of 1) the icons and/or 2) the path polyline?
 
Status
Not open for further replies.
Top