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:



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:



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:



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:

warwound

Expert
Licensed User
Longtime User
GoogleMapsExtras version 1.80 enables the display of Google StreetView in your b4a project.
Here's a couple of examples showing usage and syntax.
  • Example #1 is a basic GoogleMap that listens for map clicks and displays Google StreetView in a StreetViewPanoramaView for the clicked map point.
    You can download a working apk file for example 1 here: http://b4a.martinpearman.co.uk/googlemapsextras/streetview_example_1.apk.
  • Example #2 contains no GoogleMap, just a Spinner and a StreetViewPanoramaView.
    When the user selects a Spinner item the StreetViewPanoramaView displays a photosphere from Google Views.
    Photospheres are not official Google StreetViews they are user contributed panoramas.
    Please read the above link to find out more about photospheres.
    You can download a working apk file for example 2 here: http://b4a.martinpearman.co.uk/googlemapsextras/streetview_example_2.apk.

The StreetViewPanoramaView must be notified of changes to the Activity lifecycle.
It has various methods that enable you to do so, such as it's OnCreate OnPause and OnResume methods.
It also has methods such as OnDestroy and OnSaveInstanceState.
In a b4a project we can notify the StreetViewPanoramaView of the OnCreate, OnPause and OnResume Activity lifecycle changes but not the other changes such as OnDestroy.
In my tests this has not (yet) proved a problem but ideally we should ensure that all Activity lifecycle changes are notified to the StreetViewPanoramaView.
My solution is to sub class the android Activity class and use that sub class as my b4a Activity class using the new Extends Activity attribute:
B4X:
#Extends: uk.co.martinpearman.java.android.app.StreetViewPanoramaViewActivity
I now pass an instance of StreetViewPanoramaView to the StreetViewPanoramaViewActivity and the StreetViewPanoramaViewActivity ensures that all future changes in the Activity lifecycle are notified to the StreetViewPanoramaView.
If you do not wish to use the StreetViewPanoramaViewActivity then you must instead make calls to the StreetViewPanoramaView OnCreate, OnPause and OnResume methods when your Activity lifecycles changes.

Remember that in order to compile the examples you'll need to modify the manifest in each project to use your own API key and you'll have to compile with the private sign keystore used to obtain that API key.
You'll also need to modify the path to the Google Play Services resource files:
B4X:
#AdditionalRes: C:\Users\martin\Programming\adt-bundle-windows-x86_64-20131030\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

Martin.
 

Attachments

  • Example_1.zip
    7.3 KB · Views: 612
  • Example_2.zip
    7.8 KB · Views: 547
  • example_1.png
    265.1 KB · Views: 618
  • example_2.png
    369 KB · Views: 586

Euxaes

Member
Licensed User
Longtime User
Hello everybody, how do you guys insert a line break inside the infowindow? Is it possible?
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,

i could update my playservices lib and make all changings; the new samples are working; but now my older, before the update working code has problems, when recompiling

message from compiler:

Projection1=GoogleMapsExtras1.GetProjection(GoogleMap1)
javac 1.7.0_21
src\at\rb\b4a\gmap1\gmap.java:1189: error: cannot find symbol
mostCurrent._projection1 = mostCurrent._googlemapsextras1.GetProjection((GoogleMap)(mostCurrent._googlemap1.getObject()));
^
symbol: class GoogleMap
location: class gmap


When i remove this line, the next problem stops compiling ...
Any idea, what is wrong here?
 

warwound

Expert
Licensed User
Longtime User
@rboeck
http://www.b4x.com/android/forum/threads/googlemapsextras.26277/page-7#post-250820
Can you download GoogleMapsExtras version 1.80 and try again - looks like the library xml file got corrupted.

@Euxaes
This is a common question and there is no answer!
https://www.google.co.uk/search?q=android+google+maps+infowindow+line+break&ie=UTF-8&oe=UTF-8
See the first two results ask the same question and the only solution is to implement your own custom InfoWindow using the InfoWindowAdapter.
Look here for code examples: http://www.b4x.com/android/forum/threads/google-maps-android-v2-tutorial.24415/page-5#post-157888

Martin.
 

Euxaes

Member
Licensed User
Longtime User
Thank you for the reply, Martin. I've been searching around and saw that it's of complicated matter to implement. In the end, I ditched the idea and used another means to display the additional information. Other than the solution I went for, I'll study the implementation of custom InfoWindow which I think will be handy in some other projects.
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi all,

Is there a way to access a "longClick" event on a googlemaps marker ?

Thanks
 

GMan

Well-Known Member
Licensed User
Longtime User
Or a click event on the Marker.Title or Marker.Snippet ?
 

GMan

Well-Known Member
Licensed User
Longtime User
This is already there.

Have a look to : GmapsExt.SetOnInfoWindowClickListener(gmap, OnInfoWindowClickListener1)
Cool - Thx

But: the given example throughs an error.
i declared this in the Process_Globals
B4X:
Dim gmapsext As GoogleMapsExtras
and this in the code
B4X:
    Dim OnInfoWindowClickListener1 As OnInfoWindowClickListener
  gmapsext.SetOnInfoWindowClickListener(gmap, OnInfoWindowClickListener1)
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Thx, i forgot that ;-)
So, now it works - but how can i get i.e. which one is clicked ?
 

GMan

Well-Known Member
Licensed User
Longtime User
Thx Martin,

i tried always ...(SelectedMarker.xxx) ;-)

When trying your code, nothing happends when the Marker-Snippet is clicked
B4X:
Sub OnInfoWindowClickListener1_Click(Marker1 As Marker )
    ToastMessageShow(Marker1.Position  ,False)
End Sub

Also with MarkerOptions gives no result:
B4X:
Sub OnInfoWindowClickListener1_Click(Marker1 As MarkerOptions )
    ToastMessageShow(Marker1.GetPosition ,False)
End Sub
 

warwound

Expert
Licensed User
Longtime User
@GMan

Look in the logs, do you see a message that says 'OnInfoWindowClickListener not initialized as callback sub not found'?
If so then the OnInfoWindowClickListener is not being initialized as the library code cannot find the callback (listener) sub.

Martin.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…