Issue
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();
Solution
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;
}
}
Answered By - barto90
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.