Android Question Google Maps

Declan

Well-Known Member
Licensed User
Longtime User
I get the following error running on Android 6+:
B4X:
java.lang.RuntimeException: Object should first be initialized (GoogleMap).
My Manifest:
B4X:
'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: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"

    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)

AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.MANAGE_DOCUMENTS)

AddApplicationText(
<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="AIzaSyBnI9CipMS2Nb6HwVtT-0TUgjVkyzVYUIs"/>
)


'************ Google Play Services Base ************
AddApplicationText(
   <activity android:name="com.google.android.gms.common.api.GoogleApiActivity"
  android:theme="@android:style/Theme.Translucent.NoTitleBar"
  android:exported="false"/>
    <meta-data
  android:name="com.google.android.gms.version"
  android:value="@integer/google_play_services_version" />
)
'************ Google Play Services Base (end) ************


'End of default text.
The error is generated in this sub:
B4X:
Sub btnNearest_Click
   
    Dim CurrentLat As Double
    Dim CurrentLon As Double
   
   
   
    Dim Curs As Cursor
    Curs = Starter.SQL0.ExecQuery("SELECT * FROM config")
    Curs.Position = 0
    CurrentLat = Curs.GetDouble("lat")
    CurrentLon = Curs.GetDouble("lon")
    Curs.close
   
    LogColor("Current Lat: " & CurrentLat, Colors.Red)
    LogColor("Current Lon: " & CurrentLon, Colors.Red)


If CurrentLat = 0 And CurrentLon = 0 Then
    ToastMessageShow("You have no Current Coordinates", False)
Else

    Dim c As Cursor
    c = Starter.SQL0.ExecQuery("Select * from stations where (lat) and (lon) order by (abs(lon -(" & CurrentLon & ")) +( abs(lat -(" & CurrentLat & "))*1)) Limit 2;")
    LogColor("Records: " & c.RowCount, Colors.Blue)

    For i = 0 To c.RowCount - 1
        c.Position = i
        LogColor("Nearest : " & c.GetString("saps"), Colors.Blue)
        LogColor("Nearest Lat: " & c.GetString("lat"), Colors.Blue)
        LogColor("Nearest Lon: " & c.GetString("lon"), Colors.Blue)
        Log(CRLF)
        Dim Lat As Double
        Dim Lon As Double
        Lat = c.GetString("lat")
        Lon = c.GetString("lon")
        Dim MarkerText As String = "SAPS"
        Dim ExtraText As String = c.GetString("saps")
        gmap1 = MapFragment1.GetMap
        Dim m1 As Marker = gmap1.AddMarker(Lat, Lon, MarkerText) '<< java.lang.RuntimeException: Object should first be initialized (GoogleMap).
       
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(Lat, Lon, 10)
        gmap1.AnimateCamera(CameraPosition1)
       
        m1.Snippet = ExtraText
    Next

    pnlMap.Visible = True
End If

I have registered the API on the Google Developers Console.
 

eurojam

Well-Known Member
Licensed User
Longtime User
how did you add the mapfragment to the app, did you use the designer -> Add View-> custom View, which is the correct way to do it. If you add the mapfraqment by code - like in earlier versions of B4a it will not work and produce an error like this....that's what I am remembering...

best
stefan
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Did you wait for the MapFragment1_Ready Event?
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Thanks.
I moved the above code into: Sub MapFragment1_Ready.
Works fine and map loads no problems.
But, map loads when app is started.
Map must only load once btnNearest_Click
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Create another layout file for the map and load it when needed.

Another option is to put the map inside a panel and hide the panel until you want to show the map.
Thanks, I placed the map inside a panel and hide it until I show
 
Upvote 0
Top