Android Question GoogleMapsExtras ?

johnerikson

Active Member
Licensed User
Longtime User
Following code below crash using GoogleMapExtras!
This happens suddenly after had using the app during half a year!
I now compile with B4A 4.30
Use:Windows 7 Ultimat
lib: Core 4.01, GoogleMaps 1.02, GoogleMapExtras 1.80
GoogleMapsExtras.jar date 2014-10-20

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A GisMap
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #AdditionalRes: L:\Program\Android\adt-bundle-windows-x86_64-20140702\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

#End Region

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

Sub Process_Globals
    Dim GoogleMapsExtras1 As GoogleMapsExtras
End Sub

Sub Globals
   Dim mFragment As MapFragment
   Dim gmap As GoogleMap
   Dim MapPanel As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   MapPanel.Initialize("")
   Activity.AddView(MapPanel, 0, 0, 100%x, 100%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  
        Dim MarkerOptions1 As MarkerOptions
        MarkerOptions1.Initialize
'       
        'full function
        Dim tmpMarker As Marker
        tmpMarker= gmap.AddMarker3(64.47619, 18.3980,"Sparpkt",LoadBitmap(File.DirAssets, "ring.png")) ' Ok so far

        'but Crasch using GoogleMapsExtras below
        Dim BD As BitmapDescriptor
        Dim BF As BitmapDescriptorFactory
        BD=BF.FromAsset("ring.png")
               
        MarkerOptions1.Icon(BD)       
        MarkerOptions1.Position2(64.79619, 18.3980)
        tmpMarker=GoogleMapsExtras1.AddMarker(gmap, MarkerOptions1.Snippet("pkt").Title("1"))
'this line crash see error message, quote below
  
        Dim cp As CameraPosition
        cp.Initialize(64, 18, gmap.CameraPosition.Zoom)
        gmap.AnimateCamera(cp)
   End If
End Sub

Installing file.
PackageAdded: package:b4a.GisMap
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
map ready
true
java.lang.NullPointerException
at com.google.k.a.cj.a(Unknown Source)
at com.google.maps.api.android.lib6.c.dc.<init>(Unknown Source)
at com.google.maps.api.android.lib6.c.db.a(Unknown Source)
at com.google.maps.api.android.lib6.c.aj.<init>(Unknown Source)
at com.google.maps.api.android.lib6.c.al.a(Unknown Source)
at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at uk.co.martinpearman.b4a.googlemapsextras.GoogleMapsExtras.AddMarker(GoogleMapsExtras.java:48)
at b4a.GisMap.main._map_ready(main.java:483)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment.onViewCreated(MapFragmentWrapper.java:191)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

The problem with 'gmap.addmarker' mode are the presentation of the bitmap. In the moment I got a 1/4 (up to the left)of the hole .png file as a marker on map.

What had happen with GoogleMapExtras?
Is it possible to create symbols as markers more then using LoadBitmap(File...?
 
Last edited:

eurojam

Well-Known Member
Licensed User
Longtime User
John,
if you use a drawable instead of your ring.png from the asset folder (from the res folder) it will work:

B4X:
...
Dim AndroidResources1 As AndroidResources   
Dim MyDrawable1 As BitmapDrawable
Dim Icon1 As Bitmap
MyDrawable1=AndroidResources1.GetApplicationDrawable("ring")
Icon1=MyDrawable1.Bitmap
Dim BitmapDescriptor1 As BitmapDescriptor
Dim BitmapDescriptorFactory1 As BitmapDescriptorFactory
BitmapDescriptor1=BitmapDescriptorFactory1.FromBitmap(Icon1) 
MarkerOptions1.Icon(BitmapDescriptor1)   
MarkerOptions1.Position2(64.79619, 18.3980).Snippet("pkt").Title("1")
tmpMarker=GoogleMapsExtras1.AddMarker(gmap, MarkerOptions1)
...
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Thanks for the quick feedback!
It gave me a clue to the solution!

I don't understand what "ring" in MyDrawable1 = AndroidResources1; GetApplicationDrawable ("ring") really is!
Therefore, the program crashes on line Icon1=MyDrawable1.Bitmap.
But, I solved it like this ...
I did a class that creates bitmapdrawable, which means I don't need to use AndroidResources.
But you must be careful to create circle and rect with the correct dimensions, otherwise it will move around the map when you zooms

It would still be good to know about what that "ring" in MyDrawable1 = AndroidResources1; GetApplicationDrawable ("ring") really is?

B4X:
'Class module Create BitmapDrawable 
Private Sub Class_Globals
    Dim bdRing As BitmapDrawable   
End Sub

'Initializes the object, returns a BitmapDrawable.
Public Sub Initialize() As BitmapDrawable

    Dim r As Rect
    Dim bmp As Bitmap
    Dim cnvs As Canvas

    bmp.InitializeMutable(20,20)
    cnvs.Initialize2(bmp)

    r.Initialize(0,0,20,20)
    cnvs.DrawRect(r, Colors.Transparent, False,2)
    cnvs.DrawCircle(10,10,  10 ,Colors.Red ,False, 3)
    bdRing.Initialize( bmp)
    Return bdRing
End Sub

I change your code like this
B4X:
        Dim MarkerOptions1 As MarkerOptions
        MarkerOptions1.Initialize
        Dim tmpMarker As Marker
        Dim ring As CirclDrawAble ' class as create drawables
        'Dim AndroidResources1 As AndroidResources  
        Dim MyDrawable1 As BitmapDrawable
        Dim Icon1 As Bitmap
       
        'MyDrawable1=AndroidResources1.GetApplicationDrawable ( "ring")
        MyDrawable1 = ring.Initialize

        Icon1=MyDrawable1.Bitmap
        Dim BitmapDescriptor1 As BitmapDescriptor
        Dim BitmapDescriptorFactory1 As BitmapDescriptorFactory
        BitmapDescriptor1=BitmapDescriptorFactory1.FromBitmap(Icon1)
        MarkerOptions1.Icon(BitmapDescriptor1)  
        MarkerOptions1.Position2(64.589980, 18.683856).Snippet("pkt").Title("1")
        tmpMarker=GoogleMapsExtras1.AddMarker(gmap, MarkerOptions1)
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
John,
you made your bitmapdescriptor like this:
B4X:
BD=BF.FromAsset("ring.png")

If you copy the ring.png to the objects/res/drawable folder and put it to readonly then you can load it like
B4X:
MyDrawable1=AndroidResources1.GetApplicationDrawable("ring")
without the fileextension. But it represents the ring.png
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Yep! It works.
Now I have two or more alternative solutions to the problem, as well as further knowledge.
I noticed that the ring.png must be read-only on res\drawable else .png-file was deleted!
Thank you very much!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
noticed that the ring.png must be read-only on res\drawable else .png-file was deleted!
Thank you very much!
There is a solution about this. Search for documentation about #AdditionalRes command.
You can also have a deeper look at the AdditionalRes command in this example.
If you do it like this way all files in the resources-folder dont need to have readonly flag set. The files here are dont deleted while compiling too.
 
Upvote 0
Top