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

Cebuvi

Active Member
Licensed User
Longtime User
Hi Martin,

It is possible to know when a map has finished loading?

Thanks.

Cesar
 

warwound

Expert
Licensed User
Longtime User
Hi Martin,

It is possible to know when a map has finished loading?

Thanks.

Cesar

You can use the OnMapLoadedCallback and SetOnMapLoadedCallback method, which enables you to be notified when the map has finished rendering.

Martin.
 

Cebuvi

Active Member
Licensed User
Longtime User
You can use the OnMapLoadedCallback and SetOnMapLoadedCallback method, which enables you to be notified when the map has finished rendering.

Martin.
Martin, thanks for the quick response.

Could you give me an example of how to implement these methods in a program?

Thanks.

Cesar
 

warwound

Expert
Licensed User
Longtime User
This is the syntax to use:

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)
	If MapFragment1.IsGooglePlayServicesAvailable Then
		MapPanel.Initialize("")
		Activity.AddView(MapPanel, 0, 0, 100%x, 100%y)
		MapFragment1.Initialize("MapFragment1", MapPanel)
	Else
		Log("Google Play Services is not available")
		ToastMessageShow("Google Play Services is not available", False)
	End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub MapFragment1_Ready
	Log("MapFragment1_Ready")
	GoogleMap1=MapFragment1.GetMap
	If GoogleMap1.IsInitialized Then
	
		Dim OnMapLoadedCallback1 As OnMapLoadedCallback
		OnMapLoadedCallback1.Initialize("OnMapLoadedCallback1")
		GoogleMapsExtras1.SetOnMapLoadedCallback(GoogleMap1, OnMapLoadedCallback1)
		
		Dim CameraPosition1 As CameraPosition
		CameraPosition1.Initialize(52.7523, 0.4049, 10)
		GoogleMap1.AnimateCamera(CameraPosition1)
	Else
		Log("Error initializing GoogleMap")
		ToastMessageShow("Error initializing GoogleMap", False)
	End If
End Sub

Sub OnMapLoadedCallback1_MapLoaded
	Log("OnMapLoadedCallback1_MapLoaded")
End Sub

Have a read of this post: http://www.b4x.com/android/forum/threads/google-maps-android-v2-tutorial.24415/page-18#post-217151
The MapLoaded event is raised just once after calling SetOnMapLoadedCallback, if you need to listen for the event again - after it has already been raised - then call SetOnMapLoadedCallback again.
(I'd assume you can reuse any existing OnMapLoadedCallback but you will have to check that.

Example project attached.

Martin.
 

Attachments

  • OnMapLoadedCallback.zip
    7 KB · Views: 428

Shay

Well-Known Member
Licensed User
Longtime User
Any other ideas? since I must track user location in the background
and this library gets the location better than all others
 

Shay

Well-Known Member
Licensed User
Longtime User
Yes, this is exactly what I needed
Test and it is working, I will post some question on it on that thread
Thanks Martin
 

agus mulyana

Member
Licensed User
Longtime User
Dear all friends,

execuse me, now, i'm going to create an offline map, at least for online map, in my purpose, i want to combine GPS data with map service, but till now, i can't do that , if you never mind, please help me,....thank you so much
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
how to draw a line 7 meters centered at the current point?
You can call GMap.AddPolyline to add a line to the map.

You will need to do some calculations in order to find the line second point. You can convert the current position to UTM with the geodesic class. UTM is easier to work with as it is a linear grid.
 

westingenieria

Active Member
Licensed User
Longtime User
You can call GMap.AddPolyline to add a line to the map.

You will need to do some calculations in order to find the line second point. You can convert the current position to UTM with the geodesic class. UTM is easier to work with as it is a linear grid.

Thanks Erel, could you give me an example, please ? thanks in advanced
 

iyan

New Member
Licensed User
Longtime User
Hi Erel..


i have problems with GoogleMaps library V2, :

1. get icon of marker from link
2. infowindow marker that support with html
can you help and explain me how to implement it with B4A

this is the code in js :

B4X:
var latlng1 = new google.maps.LatLng(1.286848,103.854532);
var marker1 = new google.maps.Marker({ position:latlng1, map:map,  icon: 'http://maps.google.com/mapfiles/kml/pal2/icon5.png'});
google.maps.event.addListener(marker1, 'click',
function(){
            infowindow.close();
            infowindow.setContent('<b>MERLION</b><br>Singapore<br><br><iframe  frameborder=0 scrolling=no width=220  src=http://static6.bigstockphoto.com/thumbs/1/6/6/small2/66177976.jpg>');
            infowindow.open(map, marker1);
        }
    );

Please help me!!
sorry I am bad English.
Thanks.
 

jarnevr

New Member
Licensed User
Longtime User
When I debug this project on my device, I get an error: "Program paused on line: 31 'mFragment.IsGooglePlayServicesAvailable = False Then' "

Please help me, it's for my final project in high school.

Kind Regards
 

mkvidyashankar

Active Member
Licensed User
Longtime User
Hi

I am trying to retrieve lat and long by clicking on google map. I am using GoogleMaps library
I tried mFragment_click event. But it is not working.

Please help,
If possible please give the sample code

thanks in advance
 
Status
Not open for further replies.
Top