Android Question Google Map Marker icon Chopped

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All

While trying to scale the Marker icon to look bigger on a tablet, I ran into an issue where the Marker icon look chopped ( only a small part of it shows ).

The code below shows the working code followed by the code that will cause the issue.

B4X:
    Dim BitmapDescriptor1 As BitmapDescriptor
    Dim BitmapDescriptorFactory1 As BitmapDescriptorFactory

        'This code works ok  
    BitmapDescriptor1=BitmapDescriptorFactory1.FromAsset(TLocalMarker.TMarkerSettings.IconFile)
    MarkerOptions1.Icon(BitmapDescriptor1)
  
        'This code shows chopped icons
    'Dim bBitMap As Bitmap = LoadBitmap(File.DirAssets, TLocalMarker.TMarkerSettings.IconFile)
    'BitmapDescriptor1=BitmapDescriptorFactory1.FromBitmap(bBitMap)
    'MarkerOptions1.Icon(BitmapDescriptor1)

Can someone clarify please

Thanks in advance
 

eurojam

Well-Known Member
Licensed User
Longtime User
Put your icons (icon1.png) into the resource folder, e.g. ..\Objects\res\drawable, make them read only and then load it as an AndroidResource like this:
B4X:
Dim Markeropts As MarkerOptions
Markeropts.Initialize
Dim AndroidResources1 As AndroidResources
Dim BitmapDescriptor1 As BitmapDescriptor
Dim BitmapDescriptorFactory1 As BitmapDescriptorFactory
Dim Icon As Bitmap
Dim Mydrawable1 As BitmapDrawable
Mydrawable1=AndroidResources1.GetApplicationDrawable("icon1")
Icon=Mydrawable1.Bitmap
BitmapDescriptor1=BitmapDescriptorFactory1.FromBitmap(Icon)
Markeropts.Icon(BitmapDescriptor1)
Dim Marker1 As Marker=GoogleMapsExtras1.AddMarker(GoogleMap1, Markeropts)
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Put your icons (icon1.png) into the resource folder, e.g. ..\Objects\res\drawable, make them read only

Thanks eurojam for the reply.

But my icons are downloadable from the host, what would be the workaround?
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
What is the image size? Have you tried to resize it (search to CreateScaledBitmap)?


Ok let me clarify.
I started by using CreateScaledBitmap. That's when I encountered the issue in the first place.
To debug the issue, I started removing code line by line, then I noticed that simply by using the code commented out in my original post the problem appears.

The image I am using is 60x60 (3.34K)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try it with this code:
B4X:
Sub MapFragment1_Ready
   gmap = MapFragment1.GetMap
   Dim m1 As Marker = gmap.AddMarker3(10, 30, "test", SetBitmapDensity(LoadBitmap(File.DirAssets, "smiley.png")))
   m1.Snippet = "This is the snippet"
End Sub

Sub SetBitmapDensity(b As Bitmap) As Bitmap
   Dim jo As JavaObject = b
   Dim den As Int = Density * 160
   jo.RunMethod("setDensity", Array(den))
   Return b
End Sub
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Sub SetBitmapDensity(b As Bitmap) As Bitmap
Dim jo As JavaObject = b
Dim den As Int = Density * 160
jo.RunMethod("setDensity", Array(den))
Return b
End Sub

Thanks Erel for your continuous support.

Here is what I found out while trying the above code.

1. if I use the original image 60x60 (or 60x30) and I use the above code as is, I have the exact same results as in my original code ( the image looks too small on my tablet)

2. If I change the multiplier from "160" to "80", the images are chopped in exactly the same way the non working code in my original post did ( basically scaling the image up in size by changing the density is not working )

3. if I use a higher resolution image, say 240x120 and change the multiplier up, for example ( 320 or 640 ), the image will be scaled down in size properly, which will allow to the achieve the desired results.

My only question is: Would I run into more issues by using the larger images for display many icons on the map

Thanks again
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel

The above approach seems to be working good for resizing the icons whoever the icon location on the map seems to be off.

Here is a bit more details:
1. If I use a 60x30 icon without resizing the icon and if I use the code below, then icon location is exactly where it is supposed to be

B4X:
    Dim  mx As MarkerExtras
    mx.SetFlat(TLocalMarker.Marker,TLocalMarker.TMarkerSettings.blnFlat)
   
    If TLocalMarker.TMarkerSettings.blnAnchorToMidPoint = True Then
        mx.SetAnchor(TLocalMarker.Marker,0.5,0.5)
    End If


2. if I use a larger icon together with SetBitmapDensity as described earlier in this thread, it seems as if the icon displayed with a bit of an offset from the exact location (most likely due to the anchor point )

Any advice is greatly appreciated

Thanks in advance
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Can you upload an example?

Sorry for the delay. Here is a sample code that shows the issue

B4X:
Sub MapFragment1_Ready
  
    Log("MapFragment1_Ready")
    GoogleMap1 = MapFragment1.GetMap
  
    If GoogleMap1.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
    Else
      
            '60x30 icon
        Dim Marker2 As Marker = GoogleMap1.AddMarker3(52.75619, 0.3980, "test1",  SetBitmapDensity2(LoadBitmap(File.DirAssets, "image1.png"),160))
      
            '240 x 120
        Dim Marker3 As Marker = GoogleMap1.AddMarker3(52.75619, 0.3980, "test2",  SetBitmapDensity2(LoadBitmap(File.DirAssets, "Image2.png"),640))
      
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(52.75619, 0.3980, 25)
        GoogleMap1.AnimateCamera(CameraPosition1)

    End If
End Sub


Sub SetBitmapDensity2(b As Bitmap, Multiplier As Int) As Bitmap
   Dim jo As JavaObject = b
   Dim den As Int = Density * Multiplier
   jo.RunMethod("setDensity", Array(den))
   Return b
End Sub
 
Last edited:
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Please upload it as a project (File - Export as zip).

I am attaching a zip file as requested.
Please note that I changed the package name and removed the Map Key from the manifest file.
If you like I can email you the project including the key
 

Attachments

  • MapMarkerTest.zip
    16.8 KB · Views: 603
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why are you setting the multiplier to 640 if you want to show a large icon? It should be 160:

SS-2016-07-14_08.37.57.png
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Why are you setting the multiplier to 640 if you want to show a large icon?

Let me clarify.

If I use the small image 60x30 on a high resolution tablet the image looks very small (hard to see )
What I wanted to achieve is the following:
Declare an array of multipliers with values such as (160,320,640)
Use the larger image, as the base image, and auto select a value from the array based on the screen resolution.
So for a tablet with high resolution and bigger screen, I can use 320
And for a phone with low resolution and smaller screen , I can use 640

However the issue, is the location of the icons, which becomes even more obvious when using 640 as multiplier and even worse if you rotate the marker.

if you add this block of code after adding Marker3, you will see what I am saying:

B4X:
Dim Marker4 As Marker = GoogleMap1.AddMarker3(52.75619, 0.3980, "test4",  SetBitmapDensity2(LoadBitmap(File.DirAssets, "taxi_top_cropped1.png"),640))
Dim  mx As MarkerExtras
mx.SetRotation(Marker4,180)
 
Upvote 0
Top