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:

adrianstanescu85

Active Member
Licensed User
Longtime User
Center the gmap to the center of the panel

Hello all

I'm having a rather simple (I guess!!) problem, I just implemented google maps on my app (I'm using a Samsung Galaxy Note 10.1 tablet) and the actual map is contained inside a panel (called Panel1). This panel is somewhere on the right part of the screen, the main idea here is that it is not placed in the very center of the screen.

I turned on the GPS, got the current coordinates and animated the camera to those coordinates with a zoom of 15, and what it happens so far is that the map puts that particular target IN THE CENTER OF THE TABLET SCREEN and then zooms, instead of placing the target IN THE CENTER OF THE PANEL THAT CONTAINS THE MAP and then have the zoom. So even if the map is in fact contained within the panel, if the panel has for instance its edge close to the middle of the screen, I can see almost nothing out of my target.

How can I correct this? Please let me know, anyone.

Thank you!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
@adrianstanescu85 not sure about this. Might be a bug in Google maps API.

@AlteregoHR, this line adds a marker:
B4X:
Dim m As Marker = gmap.AddMarker(36, 15, "Hello!!!")
About the position. Add a process global variable of type CameraPosition. In Activity_Pause you should assign gmap.CameraPosition to this variable. Later in Map_Ready you should check whether this variable is initialized and if yes call gmap.MoveCamera(...).
Put it in Button_Click event.
 

adrianstanescu85

Active Member
Licensed User
Longtime User
Actually what I did find is that when writing the code,
Panel1.AddView(MapPanel, 0, 0, 100%x, 100%y)
that 100% is true NOT for the Panel object itself but for the visible part of the SCREEN.

This is true for other objects as well that use AddView.

Is this supposed to be this way, or have I just found a bug?
 

warwound

Expert
Licensed User
Longtime User
It's kind of logical..

Try:

B4X:
Panel1.AddView(MapPanel, 0, 0, Panel1.Width, Panel1.Height)

Martin.
 

adrianstanescu85

Active Member
Licensed User
Longtime User
Does anyone know what do I actually need to display for instance all hospitals on a 30km radius around a certain fixed location (current GPS position for instance)? If possible mark them with a marked, or even get their names...

Thank you!
 

GMan

Well-Known Member
Licensed User
Longtime User
Hi Erel,

implemented the maps lib in my app and all works fine - great job ! :icon_clap:
 

Laurent95

Active Member
Licensed User
Longtime User
Oo Man, i love you ! ..........

Emmmm, this words, just after the noise about the "Gay" marriage in France, hu ho...... (joke :sign0008: )
 

AlteregoHR

Member
Licensed User
Longtime User
@adrianstanescu85 not sure about this. Might be a bug in Google maps API.

@AlteregoHR, this line adds a marker:
B4X:
Dim m As Marker = gmap.AddMarker(36, 15, "Hello!!!")
About the position. Add a process global variable of type CameraPosition. In Activity_Pause you should assign gmap.CameraPosition to this variable. Later in Map_Ready you should check whether this variable is initialized and if yes call gmap.MoveCamera(...).
Put it in Button_Click event.

Thank you!
 

sioconcept

Active Member
Licensed User
Longtime User
Excuse me, i declare Globals :

B4X:
Dim mFragment       As MapFragment
Dim GMap          As GoogleMap
Dim GMapExtras      As GoogleMapsExtras
Dim InfoWindow       As OnInfoWindowClickListener
Dim MapPanel       As Panel 'Not in any Layout

And i load GMap with :

B4X:
MapPanel.Initialize("CartographieMenu")
Activity.AddView(MapPanel, 0, 0, 100%x, 100%y)
If mFragment.IsGooglePlayServicesAvailable Then
   mFragment.Initialize("Carto", MapPanel)
   MapPanel.BringToFront
End if

But, when i would like display another panel, i would like to kill mFragment and GMap because it causes a lot of freeze (i've near 10 000 markers).
 

sioconcept

Active Member
Licensed User
Longtime User
Thanks Erel, is a good way.

I've a last question about Tag; When i create a marker, i don't see "Tag" event. It's a little problem, because i can't put data (For exemple, the ID of the marker) to retreive it with Click event. For the moment, i wrote in snippet "#4520" to retreive it; so it's work of course, but it's not beautifull.
 

AlteregoHR

Member
Licensed User
Longtime User
Can anyone please help me how to use BitmapDescriptor and
BitmapDescriptorFactory to change marker icon? :sign0085:

Thanks
 

warwound

Expert
Licensed User
Longtime User
First make sure you have the GoogleMapsExtras library included in your project.

Next you need to create an instance of a BitmapDescriptor and pass it to the MarkersOptions Icon method.
You create an instance of a BitmapDescriptor by using an instance of BitmapDescriptorFactory.

Finally you create your Marker using the GoogleMapsExtras AddMarker method instead of using the GoogleMaps AddMarker, AddMarker2 or AddMarker3 method.

B4X:
Sub Process_Globals

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 MapFragment1_Ready
    Log("MapFragment1_Ready")
    GoogleMap1 = MapFragment1.GetMap
    If GoogleMap1.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
      '   use the new MarkerOptions method to create and add a Marker to the map
      
      Dim MarkerOptions1 As MarkerOptions
      MarkerOptions1.Initialize
      
      MarkerOptions1.Position2(52.75619, 0.3980).Snippet("Home is where the heart is").Title("Home Sweet Home").Visible(True)
      
      Dim BitmapDescriptor1 As BitmapDescriptor
      Dim BitmapDescriptorFactory1 As BitmapDescriptorFactory
      
      '   both of these lines load the person.png image as a custom icon
      BitmapDescriptor1=BitmapDescriptorFactory1.FromAsset("person.png")
      '   this method could be used to load a Bitmap that is located somewhere other than the assets folder
      '   BitmapDescriptor1=BitmapDescriptorFactory1.FromBitmap(LoadBitmap(File.DirAssets, "person.png"))
      
      MarkerOptions1.Icon(BitmapDescriptor1)
      
      Dim Marker1 As Marker=GoogleMapsExtras1.AddMarker(GoogleMap1, MarkerOptions1)
      
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(52.75619, 0.3980, 6)
        GoogleMap1.AnimateCamera(CameraPosition1)
    End If
End Sub

Demo project attached, note you'll need to change the API key in the manifest to run the demo.

Martin.
 

Attachments

  • BitmapDescriptor.zip
    9.2 KB · Views: 630

AlteregoHR

Member
Licensed User
Longtime User
Thank you warwound for you help. :)
I have one more question.
Is there a way to plot route between two markers like navigation from point A to point B. (like on google maps)

Thanks again for all the help :D
 
Status
Not open for further replies.
Top