Question

When I'm placing pins in MapView by using ItemizedOverlay on my Android 2.2 phone, the placement is right, but when I does it on my 4.0 and 4.1 phones, the placement is wrong. The pin is placed further down on the map, but with the same x-coordinate.

I figured out that the pixel value of the upper left corner in MapView on the 4.XX devices weren't (0.0), as it was on my 2.2 device. I could see that the difference was the height of the action bar (like the one highlighted on the image), and found some code to get the height of it. Now, when placing the pins, I subtract the height of the action bar from the y-coordinates in MapView. I get the correct result on my 4.XX devices, but now the placement is wrong on my 2.2 device.

I have an add in the top of my screen, but it's height is pretty much the same on all my devices.

Do you know what I should do to get the correct coordinates in MapView on all devices?

The code I used was:

final WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
final Display d = w.getDefaultDisplay();
final DisplayMetrics m = new DisplayMetrics();
d.getMetrics(m);
int totalHeight =mapView.getLayoutParams().height;
int statusbarHeight = totalHeight - mapView.getMeasuredHeight();

Example of action bar

Was it helpful?

Solution 2

I solved the problem, but it isn't most pretty solution.

I check for the size of the action bar. If it exist, I run the code shown in the question. If the size returns null, which means that the action bar doesn't exist, I don't run the code.

The code is:

// Check if an action bar exist (Android 4.XX) 
private void getActionBarHeight() {
    // TODO Auto-generated method stub
    final WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    final Display d = w.getDefaultDisplay();
    final DisplayMetrics m = new DisplayMetrics();
    d.getMetrics(m);
    actionBarHeight = m.heightPixels;

    try { // If the action bar exist
        TypedValue tv = new TypedValue();
        context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
        getResources().getDimensionPixelSize(tv.resourceId);
    } catch (Exception e) { // If it doesn't exist
        actionBarHeight = -1;
    }
}

OTHER TIPS

Not exactly sure what you're going for here, but if you're trying to put pins on a map, then you should use ItemizedOverlay, which takes lat/lon coords for the map, not screen coords.

There's a usage example here. That code shows how to get pins on the map, how to move them, and how to translate between screen pixels and map lat/lon, all in ~200 lines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top