Android Question Get LatLng on GoogleMaps API

Alexander Stolte

Expert
Licensed User
Longtime User
Hi,

I would like to get my current coordinates displayed without a GoogleMaps map, because the GPS class only retrieves the location when I also move, that is not in my plan.

I uploaded the app in the appstore and pointed to the API as described in this thread: https://www.b4x.com/android/forum/threads/google-maps.63930/#post-404386

My current code:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim btn_navigate As Button
    Dim btn_getlocation As Button
    Dim lbl_location As Label
    Dim lbl_loacation2 As Label
   
    Dim Maps As LatLng
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
Activity.LoadLayout("frm_main")
   
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btn_getlocation_Click(Latitude As Double, Longitude As Double)
Maps.Initialize(Latitude, Longitude)
lbl_location.Text = Maps.Latitude
lbl_loacation2.Text = Maps.Longitude


End Sub

And i get this error:

B4X:
java.lang.Exception: Sub btn_getlocation_click signature does not match expected signature.

Is it possible at all with this API to read only the coordinates without map?
 

udg

Expert
Licensed User
Longtime User
Hi,
the error means that your sub definition for btn_getlocation_click is invalid. That's due to the parameters you appended to it.
In other words, a standard button click doesn't expect parameters.

udg
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi,
I didn't run the demo, but looking at the doc for GMaps you should use:
1. a MapFragment; in its Ready event use GetMap to receive an initialized GoogleMaps object
2. GoogleMaps object (se point 1)
3. MyLocation from the GMaps object to obtain the Maps LatLng object

So your button click sub will be
B4X:
Sub btn_getlocation_Click
lbl_location.Text = Maps.Latitude
lbl_location2.Text = Maps.Longitude
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I guess you can "show" it on an invisible panel. Something like
B4X:
dim mf as mapfragment
dim gm as googlemap
mf.initialize("mymf",myinvisiblepanel)
....
sub mymf_ready
gm = mf.getmap
if gm.isinitialized then btn_getlocation.enabled=true ' since now you have a valid value for gm
end sub

Sub btn_getlocation_Click
Dim Maps As LatLng =gm.mylocation
lbl_location.Text = Maps.Latitude
lbl_location2.Text = Maps.Longitude
End Sub
The above is a schema not actual code. Please verify syntax against definitions in the doc.

BTW: I like very much your emblem for Next World Soft
 
Last edited:
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I get only errors.

like this:

B4X:
    at com.google.android.gms.maps.MapFragment$zzb.zzIL(Unknown Source)

Can I test it only when I upload it to the google play store? Because of the API?
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Sincerely, I don't know. I neither follow the tutorial nor used the lib. I just read the documentation I linked in post #4.
Being on a tablet I doubt I can be of any more help at the moment.
My suggestion is to execute the tutorial as is. Once it is working you could try the "trick" of the invisible panel.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
EDIT: It works now

Okay, the example from erel is not working, my app is chrashing by startup mhh

B4X:
#AdditionalJar: com.google.android.gms:play-services-maps

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
  
    Dim mf As MapFragment
    Dim gm As GoogleMap
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("frm_main")
    If mf.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Please install Google Play Services.", True)
    End If
  
End Sub
Sub mf_Ready
    gm = mf.GetMap
    Dim m1 As Marker = gm.AddMarker(10, 30, "test")
    m1.Snippet = "This is the snippet"
End Sub

Manifest:

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

''************ 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="AIzaSyD15Jm3je66WrWrzrj9SzSfD427Nf92F1o"/>
')
''************ Google Play Services Base (end) ************

'************ Firebase Base ************
CreateResourceFromFile("google-services", "google-services.json")
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(com.google.android.c2dm.permission.RECEIVE)
AddPermission(${applicationId}.permission.C2D_MESSAGE)
AddManifestText( <permission android:name="${applicationId}.permission.C2D_MESSAGE"
  android:protectionLevel="signature" />)
AddApplicationText(
<receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  android:exported="false"/>
   <provider
  android:authorities="${applicationId}.firebaseinitprovider"
  android:name="com.google.firebase.provider.FirebaseInitProvider"
  android:exported="false"
  android:initOrder="100" />
    <receiver
  android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
  android:enabled="true">
  <intent-filter>
  <action android:name="com.google.android.gms.measurement.UPLOAD"/>
  </intent-filter>
  </receiver>

  <service
  android:name="com.google.android.gms.measurement.AppMeasurementService"
  android:enabled="true"
  android:exported="false"/>
   <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
  android:exported="true"
  android:permission="com.google.android.c2dm.permission.SEND" >
  <intent-filter>
  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
  <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
  <category android:name="${applicationId}" />
  </intent-filter>
  </receiver>
    <receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
  android:exported="false" />


  <service
  android:name="com.google.firebase.iid.FirebaseInstanceIdService"
  android:exported="true">
  <intent-filter android:priority="-500">
  <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
  </intent-filter>
  </service>
)
'************ Firebase Base (end) ************

Why? and the log says nothing...
 
Last edited:
Upvote 0
Top