Android Question GoogleMap Marker size problem

marcick

Well-Known Member
Licensed User
Longtime User
Hi, playing again with GoogleMap library,

If I use "Mymap.AddMarker" function, the marker size is autoscaled according to the device resolution and appears of the correct dimension on any device.
If I use "MyMap.AddMarker3" to load a custom bitmap, the marker size is fixed and appears little on high resolution device or big on low resolution device.
I have tryed also with GoogleMapExtras but same results.
I have many different icons to show on the map, do I have to build several icon sets and use different set on different resolution devices or is there a better solution ?
Marco
 

marcick

Well-Known Member
Licensed User
Longtime User
That's great !
But how to obtain a bitmap that is about 5mm wide on any device ?
I would like to obtain the same behaviour of the default icon of googlemap, when you use
"Mymap.AddMarker" function
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Hi Martin,
something doesn't work.
The image "trafficlight.png" is in the folder object/res/drawable-nodpi and is read only
The code below returns always null

B4X:
    Dim AndroidResources1 As AndroidResources
    Dim GIcon As Bitmap
    Dim object1 As Object
    Dim BitmapDrawable1 As BitmapDrawable
    object1=AndroidResources1.GetApplicationDrawable("trafficlight.png")
        If object1=Null Then
        Log("Drawable NOT FOUND: "&icn)
        GIcon=Null
    Else
        BitmapDrawable1=object1
        GIcon=BitmapDrawable1.Bitmap
    End If
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Remove the .png file type:

B4X:
object1=AndroidResources1.GetApplicationDrawable("trafficlight")

Martin.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Yes, now the code works, but I still have the same problem: the icon appears little on high resolution device or big on low resolution device.
But how to obtain a bitmap that is about 5mm wide on any device ?
I would like to obtain the same behaviour of the default icon of googlemap, when you use "Mymap.AddMarker" function
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Well, using both the method of Martin and the CreateBitmapScaled I obtain acceptable results, but not perfect.
This is the full code

B4X:
icn="trafficlight"
        Dim AndroidResources1 As AndroidResources
        Dim GIcon As Bitmap
        Dim object1 As Object
        Dim BitmapDrawable1 As BitmapDrawable
        object1=AndroidResources1.GetApplicationDrawable(icn)
            If object1=Null Then
            Log("Drawable NOT FOUND: "&icn)
            GIcon=Null
        Else
            BitmapDrawable1=object1
            GIcon=BitmapDrawable1.Bitmap
        End If
        If Marker1.IsInitialized Then Marker1.Remove
        GIcon=CreateScaledBitmap(GIcon,10%x, 10%x)
        Dim BitmapDescriptor1 As BitmapDescriptor
          Dim BitmapDescriptorFactory1 As BitmapDescriptorFactory
          BitmapDescriptor1=BitmapDescriptorFactory1.FromBitmap(GIcon)
          MarkerOptions1.Icon(BitmapDescriptor1).Anchor(0.5,0.5)
        MarkerOptions1.Position2(geopoint1.Latitude,geopoint1.Longitude).Title(MarkerId).Visible(True)
          Marker1=GoogleMapsExtras1.AddMarker(gmap, MarkerOptions1)

The size of the googlemap default icon is exactely the same on any device. Here I have about 30% of difference but acceptable.
Any furhter hints ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
No, using:

B4X:
GIcon=CreateScaledBitmap(GIcon,10%x, 10%x)

is not good. On a 10 inches tablet the icon is huge ....
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Googling and googling it seems there is not a reliable way to determine the pixel per mm. Yes, there are some methods (displaymetrics) but seems to fail on some device.
So I think I will add an option in the app where the user choose small, medium or large icon and then resize the icon with CreateScaledBitmap to 32, 64 or 128 pixel.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You could get the Sclae proprty from the LayoutValues and use the routine below.
B4X:
Sub PixelsTo_mm(pix As Int) As Double
    Dim lv As LayoutValues
    lv = GetDeviceLayoutValues
    Return pix / 160 / lv.Scale * 25.4
End Sub
It's an approximate value based on the standard 160 dpi value.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Here's an idea...

Take a look at this page: http://developer.android.com/guide/practices/screens_support.html

You could put your marker icon in the folder res/drawable-mdpi instead of the res/drawable-nodpi folder, now use the AndroidResources code as above.
Does android now correctly scale the icon based on the device screen density?

If not then another solution would be the 'traditional' (java) android way.
Create folders res/drawable-mdpi, res/drawable-hdpi, res/drawable-large-hdpi and res/drawable-xlarge-mdpi, in each folder place an icon that is sized as desired for that density.
Now try the AndroidResources code again - does android load the correct icon based on the device density?

(With both of these solutions you will want to ensure that you do not have the icon placed in res/drawable-nodpi).

Martin.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Thanks Klaus and warwound, I'm busy today, I'll have a look later to the suggested solutions.
Marco
 
Upvote 0
Top