1

I am trying to plot a lat long on my screen using a small circle. This is my code:

currLat = 19.12550467;
currLong = 72.86587704;
collCurrLat = 19.1255857;
collCurrLong = 72.8660916;

cPoint c1 = GlobalMercator.LatLonToPixel(currLat,currLong, 16);
cPoint c2 = GlobalMercator.LatLonToPixel(collCurrLat,collCurrLong, 16);

int dist = GlobalMercator.distanceInMeters(c1.cx, c1.cy, c2.cx, c2.cy, 16);

int xcol =  (int) ((((MapView)mParent).getWidth()/360.0) * (180 + collCurrLong));
int ycol =  (int) ((((MapView)mParent).getHeight()/180.0) * (90 - collCurrLat));

canvas.drawCircle( xcol, ycol,GlobalMercator.meterDitanceToPixels(10,16 ), mSelectionBrush);
  1. The currLat and currLong are the lat/long of the point at the center of my screen (I plotted it using).

        int x = ((MapView)mParent).getWidth()/2;
        int y = ((MapView)mParent).getHeight()/2;
    
  2. The collCurrLat and collCurrLong are the lat/long of the near by point I need to plot on the map.

I used a method distanceInMeters() using these lat/long values to calculate the distance and it comes to be 21 meters.

But when i got the output using drawCircle in the code above, it seems that the x, y coordinates obtained using collCurrLat/collCurrLat are much farther than 21 meters (i.e. the distance between currLat/currLong and collCurrLat/collCurrLong). Also the value for xcol and ycol appear at the same location irrespective of varying lat long values! This is a rendition of my UI.

enter image description here

Can you please validate whether the approach I have taken to plot the collCurrLat/collCurrLong point is correct.

Kara
  • 6,115
  • 16
  • 50
  • 57
Angela Sim
  • 137
  • 1
  • 21

1 Answers1

0

I am not sure it is a good idea to do what you are doing. At least do not use pixels - use dp instead.

public static float convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;
}

More on units of measurment

Community
  • 1
  • 1
martynas
  • 12,120
  • 3
  • 55
  • 60
  • Thanks Martynas. Do you suggest using convertPixelsToDp method after LatLonToPixel method? What should I pass as parameter? – Angela Sim Mar 11 '14 at 08:28
  • My point is that you should not be working with pixels because your output will appear differently on each device. Do your calculations in pixels and convert to dp before rendering anything. – martynas Mar 11 '14 at 08:34
  • I suppose you are infering to accuracy.Suppose for now if we are to just analyse how i converted the lat/long to x, y coordinates (the values xcol and ycol), do you find anything missing? – Angela Sim Mar 11 '14 at 08:46