Conversion Of Screen coords To Lat and Long

Tutorials that use the MapActivity. Many using GPS functionality.

Conversion Of Screen coords To Lat and Long

Postby CHAKYANAS » Thu Apr 03, 2008 5:00 am

some simple code



//this function must be in Draw() in Overlay

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4.  
  5. Draw(Pixelcalculator pcC,------------------------)
  6.  
  7.  
  8.  
  9.  
  10.  
  11.           {            
  12.  
  13.                         int[] screenCoords = new int[2];
  14.  
  15.                         int[] screenCoords1 = new int[2];
  16.  
  17.                         Point centrePoint=new Point((int)MyMap.mMapView.getMapCenter().getLatitudeE6(),(int)MyMap.mMapView.getMapCenter().getLongitudeE6());
  18.  
  19.                         Point knownPoint=new Point((int)MyMap.mMapView.getMapCenter().getLatitudeE6()+500,(int)MyMap.mMapView.getMapCenter().getLongitudeE6()+100);
  20.  
  21.                         pxC.getPointXY(centrePoint, screenCoords);
  22.  
  23.                         pxC.getPointXY(knownPoint, screenCoords1);
  24.  
  25.                         if(screenCoords1[1]>220)
  26.  
  27.                                 eachPixelLat= 500/(screenCoords1[1]-screenCoords[1]);
  28.  
  29.                         if(screenCoords1[1]<220)
  30.  
  31.                                 eachPixelLat= 500/(screenCoords[1]-screenCoords1[1]);
  32.  
  33.                         if(screenCoords1[0]>160)
  34.  
  35.                                 eachPixelLong= 100/(screenCoords1[0]-screenCoords[0]);
  36.  
  37.                         if(screenCoords1[0]<160)
  38.  
  39.                                 eachPixelLong= 100/(screenCoords[0]-screenCoords1[0]);
  40.  
  41.                     return 0;
  42.  
  43.           }
  44.  
  45.  
  46.  
  47. //this function should be in java class where tuoch event in enabled
  48.  
  49.    public static int unknownLatLongCalc(int X,int Y)
  50.  
  51.             {
  52.  
  53.                     double  eachPixelLatDiff=eachPixelLat;
  54.  
  55.                         double  eachPixelLongDiff=eachPixelLong;
  56.  
  57.                         double centreKnownLat=mPoint.getLatitudeE6();
  58.  
  59.                         double centreKnownLog=mPoint.getLongitudeE6();
  60.  
  61.                         int  unkwPointScreenCordX=X;
  62.  
  63.                         int  unkwPointScreenCordY=Y;
  64.  
  65.                        
  66.  
  67.                        
  68.  
  69.                         //North East  
  70.  
  71.                   if(mPoint.getLatitudeE6()>0 && mPoint.getLongitudeE6()>0)
  72.  
  73.             {
  74.  
  75.                  
  76.  
  77.                   if(unkwPointScreenCordX<160)
  78.  
  79.                           unknownLog=centreKnownLog-((160-unkwPointScreenCordX)*eachPixelLongDiff);
  80.  
  81.                   if(unkwPointScreenCordY<220)
  82.  
  83.                           unknownLat=centreKnownLat+((220-unkwPointScreenCordY)*eachPixelLatDiff);
  84.  
  85.                   if(unkwPointScreenCordX>160)
  86.  
  87.                           unknownLog=centreKnownLog+((unkwPointScreenCordX-160)*eachPixelLongDiff);
  88.  
  89.                   if(unkwPointScreenCordY>220)
  90.  
  91.                           unknownLat=centreKnownLat-((unkwPointScreenCordY-220)*eachPixelLatDiff);
  92.  
  93.                        
  94.  
  95.             }  
  96.  
  97.                  
  98.  
  99.                   //North West
  100.  
  101.                   if(mPoint.getLatitudeE6()>0 && mPoint.getLongitudeE6()<0)
  102.  
  103.                   {
  104.  
  105.                          
  106.  
  107.                           if(unkwPointScreenCordX<160)
  108.  
  109.                                   unknownLog=centreKnownLog-((160-unkwPointScreenCordX)*eachPixelLongDiff);
  110.  
  111.                           if(unkwPointScreenCordX>160)
  112.  
  113.                                   unknownLog=centreKnownLog+((unkwPointScreenCordX-160)*eachPixelLongDiff);
  114.  
  115.                        
  116.  
  117.                           if(unkwPointScreenCordY<220)
  118.  
  119.                                   unknownLat=centreKnownLat+((220-unkwPointScreenCordY)*eachPixelLatDiff);
  120.  
  121.                            if(unkwPointScreenCordY>220)
  122.  
  123.                                   unknownLat=centreKnownLat-((unkwPointScreenCordY-220)*eachPixelLatDiff);
  124.  
  125.                                
  126.  
  127.                   }  
  128.  
  129.                 //South West
  130.  
  131.                   if(mPoint.getLatitudeE6()<0 && mPoint.getLongitudeE6()<0)
  132.  
  133.                   {
  134.  
  135.                          
  136.  
  137.                           if(unkwPointScreenCordX<160)
  138.  
  139.                                   unknownLog=centreKnownLog-((160-unkwPointScreenCordX)*eachPixelLongDiff);
  140.  
  141.                           if(unkwPointScreenCordX>160)
  142.  
  143.                                   unknownLog=centreKnownLog+((unkwPointScreenCordX-160)*eachPixelLongDiff);
  144.  
  145.                        
  146.  
  147.                           if(unkwPointScreenCordY<220)
  148.  
  149.                                   unknownLat=centreKnownLat+((220-unkwPointScreenCordY)*eachPixelLatDiff);
  150.  
  151.                            if(unkwPointScreenCordY>220)
  152.  
  153.                                   unknownLat=centreKnownLat-((unkwPointScreenCordY-220)*eachPixelLatDiff);
  154.  
  155.                   }  
  156.  
  157.                 //South East
  158.  
  159.                   if(mPoint.getLatitudeE6()<0 && mPoint.getLongitudeE6()>0)
  160.  
  161.                   { if(unkwPointScreenCordX<160)
  162.  
  163.                                   unknownLog=centreKnownLog-((160-unkwPointScreenCordX)*eachPixelLongDiff);
  164.  
  165.                           if(unkwPointScreenCordX>160)
  166.  
  167.                                   unknownLog=centreKnownLog+((unkwPointScreenCordX-160)*eachPixelLongDiff);
  168.  
  169.                        
  170.  
  171.                           if(unkwPointScreenCordY<220)
  172.  
  173.                                   unknownLat=centreKnownLat+((220-unkwPointScreenCordY)*eachPixelLatDiff);
  174.  
  175.                            if(unkwPointScreenCordY>220)
  176.  
  177.                                   unknownLat=centreKnownLat-((unkwPointScreenCordY-220)*eachPixelLatDiff);
  178.  
  179.                                
  180.  
  181.                   }    
  182.  
  183.                  
  184.  
  185.                  
  186.  
  187.                   return 0;
  188.  
  189.             }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
Last edited by CHAKYANAS on Thu Apr 03, 2008 9:42 am, edited 1 time in total.
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Top

Postby plusminus » Thu Apr 03, 2008 7:39 am

Hello CHAKYANAS,

did you testify this :?:

There should be a NullPointerException here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public static  int calcEachPixelLatLong()
  2.  
  3.        {
  4.  
  5.                PixelCalculator pxC = null; // Initialized with Null
  6.  
  7.                int[] screenCoords = new int[2];
  8.  
  9.                int[] screenCoords1 = new int[2];
  10.  
  11.                Point centrePoint=new Point(...);
  12.  
  13.                Point knownPoint=new Point(...);
  14.  
  15.                pxC.getPointXY(centrePoint, screenCoords); // NullPointerException Here ?!?
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Additionally you are using a lot of undocumented Numbers which are also not Constants :?

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby CHAKYANAS » Thu Apr 03, 2008 9:41 am

Hello +-



ya There is because of PixelCalcuator inside the body of the function.............

but the code inside the function must be given in Draw() of Overlay class


I ll sent u My Overlay class................



You take only the things u want


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   public  void draw(Canvas canvas, PixelCalculator pxC, boolean b) {
  2.  
  3.                  super.draw(canvas, pxC, b);
  4.  
  5.                  
  6.  
  7.                 //  calculating the Each Pixel Represents How Much Lat And Long
  8.  
  9.                                  
  10.  
  11.                 int[] screenCoords = new int[2];
  12.  
  13.                         int[] screenCoords1 = new int[2];
  14.  
  15.                        
  16.  
  17.                         Point centrePoint=new Point((int)MyMap.mMapView.getMapCenter().getLatitudeE6(),(int)MyMap.mMapView.getMapCenter().getLongitudeE6());
  18.  
  19.                         Point knownPoint=new Point((int)MyMap.mMapView.getMapCenter().getLatitudeE6()+500,(int)MyMap.mMapView.getMapCenter().getLongitudeE6()+100);
  20.  
  21.                         //Calculation Of EachPixelLat
  22.  
  23.                         pxC.getPointXY(centrePoint, screenCoords);
  24.  
  25.                         pxC.getPointXY(knownPoint, screenCoords1);
  26.  
  27.                         if(screenCoords1[1]>220)
  28.  
  29.                                 MyMap.eachPixelLat= 500/(screenCoords1[1]-screenCoords[1]);
  30.  
  31.                         if(screenCoords1[1]<220)
  32.  
  33.                                 MyMap.eachPixelLat= 500/(screenCoords[1]-screenCoords1[1]);
  34.  
  35.                         if(screenCoords1[0]>160)
  36.  
  37.                                 MyMap.eachPixelLong= 100/(screenCoords1[0]-screenCoords[0]);
  38.  
  39.                         if(screenCoords1[0]<160)
  40.  
  41.                                 MyMap.eachPixelLong= 100/(screenCoords[0]-screenCoords1[0]);
  42.  
  43. }
  44.  
  45.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4



this ll Work.........................
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Top

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests