Sub Globals
Private gmap As GoogleMap
Private MapFragment1 As MapFragment
End Sub
Sub MapFragment1_Ready
gmap = MapFragment1.GetMap
Dim lat as float
Dim lon as float
lat = gmap.MyLocation.latitude
lon = gmap.MyLocation.longitude
End Sub
If you dont need the map, just the lat and lon, then set the mapfragment visible = false. The Google Maps API is very fast on track the current pos., the GPS lib. is quite slow in my opinion.
thank you all, I am trying to apply what Alexander Stolte says, but it shows the following error:
Error occurred on line: 69 (principal)
java.lang.RuntimeException: Object should first be initialized (LatLng).
B4X:
Sub Globals
private gmap As GoogleMap
Private MapFragment1 As MapFragment
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("principal")
If MapFragment1.IsGooglePlayServicesAvailable = False Then
ToastMessageShow("Please install Google Play Services.", True)
End If
End Sub
Sub MapFragment1_Ready
Log("LLEGO A MFRAGMENT")
gmap =MapFragment1.GetMap
Dim lat As Float
Dim lon As Float
(line 69) lat = gmap.MyLocation.Latitude
lon = gmap.MyLocation.longitude
Log("LATITUD "&lat)
End Sub
error occurred on line: 69 (principal)
java.lang.RuntimeException: Object should first be initialized (GoogleMap).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.MapFragmentWrapper$GoogleMapWrapper.getMyLocation(MapFragmentWrapper.java:321)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA$1.run(BA.java:325)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Error occurred on line: 73 (principal)
java.lang.RuntimeException: Object should first be initialized (LatLng).
B4X:
Sub MapFragment1_Ready
gmap =MapFragment1.GetMap
Dim lat,lon As String
If gmap.IsInitialized = False Then
ToastMessageShow("Error initializing map.", True)
Else
(line 73) lat = gmap.MyLocation.Latitude
lon = gmap.MyLocation.longitude
gmap.AddMarker(36, 15, "Hello!!!")
Dim cp As CameraPosition
cp.Initialize(36, 15, gmap.CameraPosition.Zoom)
gmap.AnimateCamera(cp)
End If
Sub Globals
private gmap As GoogleMap
Private MapFragment1 As MapFragment
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("frm_main")
If MapFragment1.IsGooglePlayServicesAvailable = False Then
ToastMessageShow("Please install Google Play Services.", True)
End If
Dim lat as float
Dim lon as float
Wait For MapFragment1_Ready 'it waits until MapFragment1 is ready
gmap = MapFragment1.GetMap
If gmap.IsInitialized Then
Do While Not(gmap.MyLocation.IsInitialized)
Sleep(0)
Loop
lat = gmap.MyLocation.Latitude
lon = gmap.MyLocation.Longitude
End Sub
Sub MapFragment1_Ready
End Sub
My experience has shown that it does not matter whether one is in it or not, if one has internet in it finds coordinates, if he finds GPS then it refers it from there .