Search code examples
androidgoogle-mapsprojection

Android - Google Maps - Projection - toPixels() - Is it device independent pixels?


I am using the Map View Projection to obtain the screen pixels like

currentPixelLocation =  businessMapMv.getProjection().toPixels(tappedLocation, null);

Then I am using this to do some manipulation on the screen like centering a balloon tip.

So what I do is

currentPixelLocation.y = currentPixelLocation.y - 100

This works fine. Are the pixel locations returned by the toPixels method device independent ?

Will my manioulation like the above code work for all screen resolutions ?


Solution

  • I think there is a misunderstanding in device independent pixels on your site. If you declare your layout in device independent pixels (dip), the framework itself calculates the correct Veiw size - based on device display density - for the device it running on at runtime. After the framework calculates the View dimension, the View has its dimension set in pixels.

    So therefore, getProjection().toPixels() gives you the position relative to the underlaying MapView in pixels. Those pixels are device independent.

    What seems wrong to me is you calculation currentPixelLocation.y = currentPixelLocation.y - 100 What does the 100 stand for? These 100 are definately device dependent. If you want to subtract 100 pixels, just use this calculation currentPixelLocation.y = currentPixelLocation.y - 100 * getResources().getDisplayMetrics().density + 0.5f. It ensures, that the 100 pixels are calculated device *in*dependent.