Android Question Map View (Gmaps V2) - Zoom to location?

alexwekell

Member
Licensed User
Longtime User
I noticed that the tutorial on here adds a marker to google maps and zooms on it. How exactly do I go about making it so when the mapview is initiated it zooms in on the users location instead?

Edit: forgot to add my code, but this should zoom on the users location, right?

B4X:
Sub Map_Ready
mylocation.Initialize(lat,lng)
lng = mylocation.Longitude
lat = mylocation.Latitude
  Log("map ready")
  gmap = mFragment.GetMap
  If gmap.IsInitialized = False Then
      ToastMessageShow("Error initializing map.", True)
  Else
      gmap.MyLocationEnabled = True
      Dim cp As CameraPosition
      cp.Initialize(lat, lng, gmap.CameraPosition.Zoom)
      gmap.AnimateCamera(cp)
  End If
End Sub
 
Last edited:

alexwekell

Member
Licensed User
Longtime User
B4X:
Sub Map_Ready
   Log("map ready")
   gmap = mFragment.GetMap
   If gmap.IsInitialized = False Then
      ToastMessageShow("Error initializing map.", True)
   Else
     gmap.MyLocationEnabled = True
      Dim cp As CameraPosition
     
     userlocation.Initialize(lat,lng)
        If userlocation.IsInitialized = True Then
            lng = userlocation.Longitude
            lat = userlocation.Latitude
        Else
            Msgbox("Location Unavailable","Error")
        End If
       
      cp.Initialize(lat, lng, gmap.CameraPosition.Zoom)
      gmap.AnimateCamera(cp)
   End If
End Sub

B4X:
Sub Globals

  Dim mFragment As MapFragment
  Dim gmap As GoogleMap
  Dim MapPanel As Panel
  Dim userlocation As LatLng
  Dim lat As Double
  Dim lng As Double

Lat and Lng always come out to 0.0. Could it be because I am testing my app inside of a building? I thought it would zoom in on my location using Google's WiFi data also.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Every time you run ,you initialize userlocation.Initialize(lat,lng), what are the values of lat and lng ?
Then you test if userlocation is initialized ? It is of course you just did it ?
And set lat and lng with the previous values ?
 
Last edited:
Upvote 0

alexwekell

Member
Licensed User
Longtime User
Every time you run ,you initialize userlocation.Initialize(lat,lng), what are the values of lat and lng ?
Then you test if userlocation is initialized ? It is of course you just did it ?
Ans set lat and lng with the previous values ?

B4X:
  If gmap.IsInitialized = False Then
      ToastMessageShow("Error initializing map.", True)
  Else
  Dim cp As CameraPosition
  gmap.MyLocationEnabled = True
  cp.Initialize(gmap.MyLocation.Latitude, gmap.MyLocation.Longitude, gmap.CameraPosition.Zoom)
    gmap.AnimateCamera(cp)
          End If
  End Sub

Returns the error:

LastException java.lang.RuntimeException: Object should first be initialized (LatLng)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
From the help file for GoogleMaps.MyLocation: Returns the current location. Will return an uninitialized object if the location is not available.
So as you get an error for a none initialized object this means that MyLocation is not available.
You need to check why it is not available.
Test the program outsides to make sure that you get GPS satellites.
 
Upvote 0

alexwekell

Member
Licensed User
Longtime User
Ok I finally figured it out using a GPS listener. Dim gps1 as a gps and then add this sub:

B4X:
Sub Activity_Resume
    If gps1.GPSEnabled = False Then
    Msgbox("Error message here","Error Msg")
    Else
    gps1.Start(0,0)
    End If
End Sub

and this sub:

B4X:
    Sub gps1_locationchanged (Location1 As Location)
    Dim cp As CameraPosition
    cp.Initialize(Location1.Latitude, Location1.Longitude, gmap.CameraPosition.Zoom)
    gmap.AnimateCamera(cp)
 
    End Sub
 
Last edited:
Upvote 0
Top