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:

mkvidyashankar

Active Member
Licensed User
Longtime User
Yes got it, some typo mistake in event , sorry

can we get list of all markers, marked on the map
i searched in google map library and googlemapextras
pl let me know,
 

Croïd

Active Member
Licensed User
Longtime User
Please, it is mandatory to use Google Play Services location (Location_API)

"Error occured (LatLng) "


B4X:
GPS = p.GetSettings("location_providers_allowed")
   If GPS.IndexOf("gps")> -1  Then
     gmap.MyLocationEnabled = True
       cp.Initialize(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude,18)
      gmap.AnimateCamera(cp)
   Else
       cp.Initialize(48.858557, 2.294524, 6)
       gmap.AnimateCamera(cp)
   End If

With this code :

https://www.b4x.com/android/forum/threads/map-view-gmaps-v2-zoom-to-location.40016/#post-238939

Problem with Gps changed, camera always follows Mylocation !

i want only zoom camera one time as maps
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Can I load a custom map from My maps service? Also I'd like to add markers in the custom map...

Thanks!

'My Maps' will be a KML file will it or do they have other formats that you can export and use?

As far as i know there's currently no way to automatically render a KML file on a Google Map.
KML is an XML based format so there's no reason you couldn't parse it and get the various features and then add the features using Markers, Polylines and Polygons.
 

Croïd

Active Member
Licensed User
Longtime User
Sorry, It's so easy !!

B4X:
 Dim cp As CameraPosition
    cp.Initialize(Location1.Latitude, Location1.Longitude, 16)
    gmap.MoveCamera(cp)
    GPS1.Stop
 
Last edited:

jarnevr

New Member
Licensed User
Longtime User
mostly i get an error (which i forgot to screenshot) but now i get this empty maps screen, but i tried everything to fix it, and it still won't work. Can you please help me?

screen:
 

SCIS

Active Member
Licensed User
Longtime User
Hello Erel,

Thank you for helping Jarnevr with this problem. We've tried the sample apk and this work on the device we are using for debugging. That's the good news.

I've created a new 'clean' project and followed the tutorial to the letter and, just like Jarne, I am getting the white screen. Attached to this post you'll find an image with the api and keys. I don't see something wrong, maybe you will.

The tutorial mentiosn that there could be a problem with the package name, sign key and the API key. I can't seem to find where I have to put the package name in B4A.

Any help will be greatly appreciated.

Best regards,
Geert
 

Attachments

  • StuTrackApiSetup.png
    110.9 KB · Views: 298

Jaime Ramirez

Member
Licensed User
Longtime User
Erel:

I am working with Google Maps tutorial and compiling the application with <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="19"/> While downloading the package to the device I am getting this error "There was a problem parsing the package".

Any Ideas?

Jaime
 

Jaime Ramirez

Member
Licensed User
Longtime User
Erel:

Attached please find the google map project I am working on (SDK V 19), it is your sample in the tutorial, the package name is SIOS.NTP and the API key was generated using it, your googlemaps.APK file works fine in my device (Moto G 4LTE Android 4.4.4). The code compiles just fine with no error but when it downloads to the device I am getting an error "There was a problem parsing the package).

Any Ideas?

Jaime
 

Attachments

  • NTP.zip
    1.7 KB · Views: 314

Jaime Ramirez

Member
Licensed User
Longtime User
Hi:

I have repeated the process step by step and I keep getting the same error "There was a problem parsing the package", Attached is the project files. Any ideas?,

Regards

Jaime
 

Attachments

  • SIOS_NTP.zip
    3.5 KB · Views: 306

Jaime Ramirez

Member
Licensed User
Longtime User
Hi:

Dunno why the manifest code was not there, I zipped the whole project. Any way I have it running, this is the manifest code I am using now, for other members to use it as a reference.

Regards

Jaime

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
android:name="$PACKAGE$.permission.MAPS_RECEIVE"
androidrotectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddApplicationText(<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDDnrglJT7hyvdmWHCxxxxxxxx"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@Integer/google_play_services_version"
/>)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
'End of default text.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…