1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Tutorial (old) Google Maps Android v2 tutorial

Discussion in 'Tutorials & Examples' started by Erel, Dec 18, 2012.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    You will need to use a different activity for the map.
     
  2. wes58

    wes58 Active Member Licensed User

    I thought about it after I posted a question, but it complicates things since the map doesn't cover the whole screen, there are buttons and labels (tabs) which I don't want to cover and I want to have access to.
     
  3. wheretheidivides

    wheretheidivides Active Member Licensed User

    Why does everything take 100 steps to do? hmm. It worked. Cool. My phone has 2.1 but my tablet 3+. Now if I can figure out how to set locations. Good job erel as always.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    There are 5 and half steps in the tutorial.
     
  5. George Popovic

    George Popovic Member Licensed User

    Error Initializing Map

    Hi,

    I am going throught this tutorial and I get a map to show but no marker.
    The log does not show "Map Ready" so Sub Map Ready does not get executed. Any ideas??? Thanks
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. George Popovic

    George Popovic Member Licensed User

    Tried the suggested APK, same thing. I get a map to show but no marker.
    The log does not show "Map Ready". The device is a Motorola tablet.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Try to update Google Maps application.
     
  9. magarcan

    magarcan Active Member Licensed User

    Hi!

    I've some questions about that :eek:
    -Do I need to have installed also Google Map app in order to use mine?
    -Can I add a Placemark with it's own Rich text description (I mean text, images...)?
    -Can I list all Placemarks in my map, for example, 12Km around any position?

    Maybe some of that questions are answered in the documentation, where can I find it?

    Cheers!
     
    Last edited: Feb 6, 2013
  10. Erel

    Erel Administrator Staff Member Licensed User

    A link to Google's documentation is available in the first post.

    The library documentation is available in the first link:
    Basic4android Search: GoogleMaps
     
  11. magarcan

    magarcan Active Member Licensed User

    I've taken a look here:Basic4android - GoogleMaps
    It seems, AddMarker doesn't support descriptions...
    And nothing about get other markers info...
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    You can set the description with Marker.Snippet.
     
  13. warwound

    warwound Expert Licensed User

    Due to popular demand(!!) i have created the GoogleMapsExtras library adding support for more Google Maps Android v2 features in b4a.
    GoogleMapsExtras library files can be found in this thread: http://www.basic4ppc.com/forum/addi...pdates/26277-googlemapsextras.html#post152004

    This example shows the syntax to use to add a custom TileOverlay to your map.
    The example displays tiles from the OpenStreetMap project.

    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
        
    Dim MapFragment1 As MapFragment
        
    Dim GoogleMap1 As GoogleMap
        
    Dim MapPanel As Panel
       
    Dim TileOverlay1 As TileOverlay
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    Activity.LoadLayout("Main")
        
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
            
    ToastMessageShow("Google Play services not available."True)
        
    Else
            MapFragment1.Initialize(
    "MapFragment1", MapPanel)
        
    End If 
    End Sub

    Sub MapFragment1_Ready
        
    Log("MapFragment1_Ready")
        GoogleMap1 = MapFragment1.GetMap
        
    If GoogleMap1.IsInitialized = False Then
            
    ToastMessageShow("Error initializing map."True)
        
    Else
          
    '   set the map-type to none
          '   the new TileOverlay is not transparent and we do not want to load any of the built in tile sources
          GoogleMap1.MapType=GoogleMap1.MAP_TYPE_NONE
          
          
    Dim GoogleMapsExtras1 As GoogleMapsExtras
          
    Dim TileOverlayOptions1 As TileOverlayOptions
          
    Dim UrlTileProvider1 As UrlTileProvider
          
          
    '   Url parameters are %1$d tileX, %2$d tileY, %3$d zoom
          '   OSM slippy map tilename convention is <url>/zoom/x/y.png
          
          UrlTileProvider1.Initialize(
    "http://b.tile.openstreetmap.org/%3$d/%1$d/%2$d.png"256256)
          TileOverlayOptions1.Initialize
          TileOverlayOptions1.SetTileProvider(UrlTileProvider1)
          TileOverlay1=GoogleMapsExtras1.AddTileOverlay(GoogleMap1, TileOverlayOptions1)
          
          
    Dim Marker1 As Marker
            Marker1=GoogleMap1.AddMarker(
    52.756190.3980"Home Sweet Home")
            
    Dim CameraPosition1 As CameraPosition
            CameraPosition1.Initialize(
    52.756190.39806)
            GoogleMap1.AnimateCamera(CameraPosition1)
        
    End If
    End Sub

    Sub ClearCacheButton_Click
       TileOverlay1.ClearTileCache
    End Sub

    Sub RemoveButton_Click
       TileOverlay1.Remove
    End Sub

    Sub VisibilityButton_Click
       TileOverlay1.Visible=
    Not(TileOverlay1.IsVisible)
    End Sub
    I disable the built in tile sources using GoogleMap1.MapType=GoogleMap1.MAP_TYPE_NONE and the OSM tiles display on their own.
    If you had a semi-transparent set of custom tiles you'd probably enable one of the built in tile sources (road map, satellite etc) and then display your semi-transparent tiles over the built in tile source.

    You can display tiles from a tile server if you can pass a suitable UrlTemplate String to the UrlTileProvider Initialize method.
    The UrlTemplate contains placeholders which get replaced each time a tile is requested by the map.
    In the example, the UrlTemplate is http://b.tile.openstreetmap.org/%3$d/%1$d/%2$d.png.
    The placeholders represent:
    • %1$d tileX coordinate.
    • %2$d tileY coordinate.
    • %3$d zoom level.

    There's another public tile server at: Free Relief Layers for Google Maps and you could use the maps-for-free tiles with a UrlTemplate of http://www.maps-for-free.com/layer/relief/z%3$d/row%2$d/%3$d_%1$d-%2$d.jpg.
    You'll probably notice that these public tile servers are far slower to serve tiles than the Google Maps tile servers - Google have the capital to pay for lots of very fast tile servers and a very fast connection to the internet, the public tile servers are generally funded by donations and haven't got the resources needed to compete with Google.

    I haven't experimented yet to see if we can use a local file based Url for the tiles, in theory it'd work but needs to be tested...

    Martin.
     

    Attached Files:

  14. warwound

    warwound Expert Licensed User

    This example shows how to add a GroundOverlay to your map.
    The example replicates the official javascript Google Maps API example at https://google-developers.appspot.com/maps/documentation/javascript/examples/groundoverlay-simple

    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
        
    Dim MapFragment1 As MapFragment
        
    Dim GoogleMap1 As GoogleMap
        
    Dim MapPanel As Panel
       
    Dim GroundOverlay1 As GroundOverlay
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    Activity.LoadLayout("Main")
        
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
            
    ToastMessageShow("Google Play services not available."True)
        
    Else
            MapFragment1.Initialize(
    "MapFragment1", MapPanel)
        
    End If 
    End Sub

    Sub MapFragment1_Ready
        
    Log("MapFragment1_Ready")
        GoogleMap1 = MapFragment1.GetMap
        
    If GoogleMap1.IsInitialized = False Then
            
    ToastMessageShow("Error initializing map."True)
        
    Else
          
    '   replicate the web based API GroundOverlay example at: https://google-developers.appspot.com/maps/documentation/javascript/examples/groundoverlay-simple
          GoogleMap1.MapType=GoogleMap1.MAP_TYPE_NORMAL
          
          
    Dim GoogleMapsExtras1 As GoogleMapsExtras
          
    Dim GroundOverlayOptions1 As GroundOverlayOptions
          
    Dim LatLngBounds1 As LatLngBounds
          
    Dim SouthWest, NorthEast As LatLng
          
          SouthWest.Initialize(
    40.712216,-74.22655)
          NorthEast.Initialize(
    40.773941,-74.12544)
          LatLngBounds1.Initialize(SouthWest, NorthEast)
          GroundOverlayOptions1.Initialize
          GroundOverlayOptions1.Image(
    LoadBitmap(File.DirAssets, "newark_nj_1922.jpg")).PositionFromBounds(LatLngBounds1).SetTransparency(0.2)
          GroundOverlay1=GoogleMapsExtras1.AddGroundOverlay(GoogleMap1, GroundOverlayOptions1)
          
            
    Dim CameraPosition1 As CameraPosition
            CameraPosition1.Initialize(
    40.7395, -74.17512)
            GoogleMap1.AnimateCamera(CameraPosition1)
        
    End If
    End Sub

    Sub RemoveButton_Click
       GroundOverlay1.Remove
    End Sub

    Sub VisibilityButton_Click
       GroundOverlay1.Visible=
    Not(GroundOverlay1.IsVisible)
    End Sub
    One thing to note is that you'll run into the common out of memory exceptions if you try to use a large image as a ground overlay.
    I did experiment with adding the manifest attribute largeHeap and that enabled me to use a large bitmap where previously (without largeHeap) the Activity force closed with an out of memory exception.
    More details about that can be found in this thread: http://www.basic4ppc.com/forum/addi...ogle-map-library-tile-overlay.html#post149931.

    If you have a lot of imagery to overlay on to the map then a TileOverlay seems the better solution.

    Martin.
     

    Attached Files:

    Last edited: Feb 12, 2013
  15. warwound

    warwound Expert Licensed User

    This example shows partial support for the Polygon object.
    I haven't had time to add the code required to enable holes in polygons, so currently you can display just basic simple polygons.
    (Hopefully i'll add support for holes within the next week).

    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
        
    Dim MapFragment1 As MapFragment
        
    Dim GoogleMap1 As GoogleMap
        
    Dim MapPanel As Panel
       
    Dim Polygon1 As Polygon
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    Activity.LoadLayout("Main")
        
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
            
    ToastMessageShow("Google Play services not available."True)
        
    Else
            MapFragment1.Initialize(
    "MapFragment1", MapPanel)
        
    End If 
    End Sub

    Sub MapFragment1_Ready
        
    Log("MapFragment1_Ready")
        GoogleMap1 = MapFragment1.GetMap
        
    If GoogleMap1.IsInitialized = False Then
            
    ToastMessageShow("Error initializing map."True)
        
    Else
          GoogleMap1.MapType=GoogleMap1.MAP_TYPE_NORMAL
          
          
    Dim GoogleMapsExtras1 As GoogleMapsExtras
          
    Dim PolygonOptions1 As PolygonOptions
          
    Dim Point1, Point2, Point3, Point4, Point5 As LatLng
          
    Dim Points As List
          
          Points.Initialize
          
          
    '   points must be ordered in a  counterclockwise order
          '   (i found FillColor will fail if not correctly ordered)
          
          Point1.Initialize(
    52.7560.400)
          Points.Add(Point1)
          Point2.Initialize(
    52.4170.753)
          Points.Add(Point2)
          Point3.Initialize(
    52.6381.291)
          Points.Add(Point3)
          Point4.Initialize(
    52.9020.617)
          Points.Add(Point4)
          Point5.Initialize(
    52.7560.400)
          Points.Add(Point5)   
    '   closes the path
          
          PolygonOptions1.Initialize
          PolygonOptions1.FillColor=
    Colors.ARGB(12825500)
          PolygonOptions1.Points=Points
          PolygonOptions1.StrokeColor=
    Colors.Black
          PolygonOptions1.StrokeWidth=
    2
          
          Polygon1=GoogleMapsExtras1.AddPolygon(GoogleMap1, PolygonOptions1)
          
            
    Dim CameraPosition1 As CameraPosition
            CameraPosition1.Initialize(
    52.6890.77010)
            GoogleMap1.AnimateCamera(CameraPosition1)
        
    End If
    End Sub

    Sub RemoveButton_Click
       Polygon1.Remove
    End Sub

    Sub VisibilityButton_Click
       Polygon1.Visible=
    Not(Polygon1.Visible)
    End Sub
    The map displays a polygon with 4 vertices over Norfolk, UK.
    The polygon has a 2 pixel wide black border and a semi-transparent red fill color.

    Note that when adding points you must order the points in a counterclockwise direction.
    I didn't do that originally and the polygon failed to display any fill color.
    Correctly ordering the points fixed that and the fill color was displayed.

    Martin.
     

    Attached Files:

  16. rboeck

    rboeck Well-Known Member Licensed User

    Hi,
    sorry for my notunderstanding: what steps from posting 1 are needed to use the demo projects. They are ready signed or do i have to change them to my signatures. At this time i dont want to make my own project, i want only study the possibilities...
    Greetings
    Reinhard
     
  17. warwound

    warwound Expert Licensed User

    I was forgetting about the key signing 'hassle' required...

    I'd guess that none of the example projects i uploaded will run as they are - my API key will not be valid for other users.

    So you could take a working project such as the project you created from post #1 in this thread, and copy/paste my example code into that working project.
    (Note that my examples all use a .bal layout file which can be seen in the Files tab, the GroundOverlay example also requires a .jpg which can also be seen in the files tab).

    Or you could open my examples and go though the process of obtaining a valid API key to compile my examples on your computer.

    (What a hassle lol).

    Martin.
     
  18. magi6162

    magi6162 Well-Known Member Licensed User

    Geocoder

    hi,
    I have tried your example all ok.
    I have added geocoding using the libraryGeocoder, it doesn't find address (Tried: Florence)

    Code:
    #Region  Project Attributes 
       
    #ApplicationLabel: B4A Example
       
    #VersionCode: 1
       
    #VersionName
       
    'SupportedOrientations possible values: unspecified, landscape or portrait.
       #SupportedOrientations: unspecified
       
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes 
       
    #FullScreen: False
       
    #IncludeTitle: True
    #End Region

    'Activity module
    Sub Process_Globals
       
    Dim Geocoder1 As Geocoder

    End Sub

    Sub Globals
        
    Dim mFragment As MapFragment
        
    Dim gmap As GoogleMap
        
    Dim MapPanel As Panel
    '   Dim ResultsList As ListView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       Geocoder1.Initialize(
    "Geocoder1")
        MapPanel.Initialize(
    "")
        
    Activity.AddView(MapPanel, 00100%x100%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(
    3615"Hello!!!")
          
            
    Dim cp As CameraPosition
            cp.Initialize(
    3615, gmap.CameraPosition.Zoom)
            gmap.AnimateCamera(cp)
        
    End If
       
    'ResultsList.Clear
       
       
    Dim LocationName As String
       LocationName=
    "Firenze"
       
       
    '   MaxResults parameter hardcoded to 5 for the demo
       Dim MaxResults As Int
       MaxResults=
    1
       
       Geocoder1.GetFromLocationName(LocationName, MaxResults, 
    Null)
    End Sub
    Sub Geocoder1_GeocodeDone(Results() As Address, Tag As Object)
       
    If Results.Length>0 Then
          
    Dim Address1 As Address
          
    Dim i As Int
          
    For i=0 To Results.Length-1
             Address1=Results(i)
             gmap.AddMarker(Address1.Latitude, Address1.Longitude, 
    "Firenze")
             
    'ResultsList.AddSingleLine("("&Address1.Latitude&", "&Address1.Longitude&")")
          Next
       
    Else
          
    Msgbox("GetFromLocation""No Latitude and Longitude found for the LocationName")
       
    End If
    End Sub
    How can I make geocoding?

    Help me

    Best regards
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    You should start a new thread for this question. If I understand it correctly then it is not really related to GoogleMaps object.
     
  20. ErickAsas

    ErickAsas Member Licensed User

    Marker DragEnd Events and Labels

    Hi Erel,

    How do you implement DragEnd event with the new google map library?

    Can you post an example where you can customize the contents of the info window? ie, bold text, line breaks etc...

    :sign0104:
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice