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

warwound

Expert
Licensed User
Longtime User
Well there's the Polyline object that can be used to draw a route BUT no built in funtionality to actually get a route from A to B.

I did start a new b4a library that wrapped the android OSMBonuspack library a few weeks ago.
It enables you to get directions from OSRM, MapQuest or Google servers.
The directions could then be drawn on an OSMDroid or Google map.

Currently it's development is on hold pending me finding enough time to work more on it.

So your only real solution at the moment is to find a web based routing service and make your own queries using the HTTP library.

Martin.
 

nico78

Active Member
Licensed User
Longtime User
Hi,

I try the example and i have always this message""Google Play services not available."

How many times for commissioning of API Key?
 

AlteregoHR

Member
Licensed User
Longtime User
Thank you Martin for quick replay.
Will try with HTTP lib...
OSMBonuspack looks interesting, looking forward to try it when it is finished. :)
 

warwound

Expert
Licensed User
Longtime User
Thank you Martin for quick replay.
Will try with HTTP lib...
OSMBonuspack looks interesting, looking forward to try it when it is finished. :)

If you'd like to help test the new library then send me a PM and i'll let you have a copy of the library as it is now.
Currently it should be able to query either OSRM, MapQuest or Google for directions.
What it currently cannot do is automatically draw the received directions on a map - it'll return an object that contains the directions and you'd need to draw the directions yourself.

Even in it's present state it will be useful i think and save you having to create code to query an online service and then parse the response into some useable format.

Martin.
 

maleche

Active Member
Licensed User
Longtime User
Please assist with Google Map install problem.

Wow.. I worked for 8 hours and still could not get the Google map app to work.
I copied all the required files to Library, updated the SHA1 key into the manifest, posted the SHA1 and app name and got the key and entered it into the manifest, compiled (good).
After installing the app I receive "Sorry! The application b4a.googlemaptest (process b4a.googlemaptest) has stopped unexpectedly. Please try again"

Any help is GREATLY appreciated!

Below is the catlog error message:

Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:0b4a.googlemaptest
** Activity (main) Crea0te, isFirst = true **
main_activity_create 0(java line: 249)
java.lang.NoSuchMethodError: android.app.Activity.getFragmentManager
at anywheresoftware.b4a.objects.MapFragmentWrapper.Initialize(MapFragmentWrapper.java:115)
at b4a.goo0glemaptest.main._activity_create(main.java:249)
at java.la0ng.reflect.Method.invokeNative(Native Method)
at java.0lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.googlemaptest.main.afterFirstLayout(main.java:89)
at b4a.googlemaptest.main.access$100(main.java:16)
at b4a.googlemaptest.main$WaitForLayout.run(main.java:74)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at and0roid.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
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:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.googlemaptest
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 249)
java.lang.NoSuchMethodError: android.app.Activity.getFragmentManager
at anywheresoftware.b4a.objects.MapFragmentWrapper.Initialize(MapFragmentWrapper.java:115)
at b4a.googlemaptest.main._activity_create(main.java:249)
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 b4a.googlemaptest.main.afterFirstLayout(main.java:89)
at b4a.googlemaptest.main.access$100(main.java:16)
at b4a.googlemaptest.main$WaitForLayout.run(main.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:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
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:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 

maleche

Active Member
Licensed User
Longtime User
Thanks for responding.

I used the GoogleMap.zip code from Erel's example.

Still not working.

Any ideas based on the error codes from CatLog?
 

dualznz

Member
Licensed User
Longtime User
i followed the instuctions to create api key, done that added the Google Maps Android API v2 but when i goto the API ACCESS / CREATE NEW ANDOID KEY, add my SH1 key it comes back saying its invalid?

not sure what im doing wrong.

cheers

UPDATE
never mind i forgot to add ;com.example at the end of it
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Hi Martin,
I using Droid Razr, Droid X, and SAMSUNG Galaxy.

I'm not familiar with the Droid models, Google says the Droid X runs Gingerbread though the Razr looks to runs a newer version of Android.

There was a request for a version of the GoogleMaps library that supported Android versions 2.?, see here: http://www.b4x.com/forum/basic4andr...e-maps-android-v2-tutorial-12.html#post166448.
No one though has taken on the task of making a 2.? compatible library.

Martin.
 

CapReed

Member
Licensed User
Longtime User
Good night,

I want a photo displayed outside when tap the descriptor.

Is there any method to show a photo when you click the descriptor?

I think another more traditional method would be to use OnInfoWindowClickListener and make visible some imageview which would load the external image. Would it be this correct?

Maybe I will this have you tried asking something earlier ... :sign0013:
 

PaulD

Member
Licensed User
Longtime User
Google Maps MyLocation and Start Zoom Level

I have been pouring over the community and I have not been able to find anything that will help me with these two items...

1. How does "MyLocation" and work.. can I have the basic Google Maps show my current location on the map? Or do I need to be looking at "GeoLocation'?

2. When the Google Map starts it is viewing whole nations... how can I make it start at the city level?

Thank you!
 

PaulD

Member
Licensed User
Longtime User
You should set MyLocationEnabled to True.
You can use MyLocation to set the camera position. The camera position also includes a zoom property which you can use to change the zoom.

do you have a coded example I can work from? I have been trying different commands but for some reason I can't seem to make it work right...
 
Status
Not open for further replies.
Top