getting onKeyDown to work?

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

getting onKeyDown to work?

Postby -A- » Mon Mar 14, 2011 2:05 pm

My aim here is that I want this activity to be able to move around and show the toasts while the user navigates the map, but as soon as he hits the center key, it should return the last value that the user touched. Its not working for some reason, and when I put onKeyDown inside the if loop, it gives errors. How do I get it to work? Its really frustrating :( . Thanks.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class createNote extends MapActivity {
  2.  
  3. MapView mapview;
  4. String sLatt;
  5. String sLonn;
  6.  
  7. @Override
  8. protected boolean isRouteDisplayed() {
  9.     return false;
  10. }      
  11.  
  12. @Override
  13. public void onCreate(Bundle savedInstanceStates){      
  14.     super.onCreate(savedInstanceStates);
  15.     setContentView(R.layout.map);
  16.  
  17.     mapview=(MapView)findViewById(R.id.MapView);
  18.     mapview.setBuiltInZoomControls(true);
  19.  
  20.     mapOverlay mapoverlay=new mapOverlay();
  21.     List<Overlay>listOfOverlays=mapview.getOverlays();
  22.     listOfOverlays.add(mapoverlay);
  23.  
  24.  
  25. }  
  26.  
  27.  
  28. class mapOverlay extends com.google.android.maps.Overlay{
  29.     @Override
  30.  
  31.     public boolean onTouchEvent(MotionEvent event, MapView mapview){
  32.         GeoPoint p=null;
  33.  
  34.         if (event.getAction()==1){
  35.              p=mapview.getProjection().fromPixels((int)event.getX(), (int)event.getY());
  36.             Toast.makeText(getBaseContext(),p.getLatitudeE6()/1E6 + "," + p.getLongitudeE6()/1E6, Toast.LENGTH_SHORT).show();
  37.  
  38.  
  39.  
  40.         double lat=(double) (p.getLatitudeE6()/1E6);
  41.         double lon=(double) (p.getLongitudeE6()/1E6);
  42.  
  43.         String sLat=Double.toString(lat);
  44.         String sLon=Double.toString(lon);
  45.         sLatt=sLat;
  46.         sLonn=sLon;
  47.  
  48.         Toast.makeText(getBaseContext(),sLat + "-" + sLon,Toast.LENGTH_SHORT).show();
  49.  
  50.  
  51.         //setResult(RESULT_OK, new Intent().setAction(sLat + "," + sLon));
  52.         //setResult(RESULT_OK, new Intent().setAction("test"));
  53.         //Intent data = new Intent();
  54.         //data.putExtra("key1", sLat);
  55.         //data.putExtra("key2", sLon);
  56.         //setResult(RESULT_OK,data);
  57.         //finish();
  58.  
  59.  
  60.  
  61.         }
  62.  
  63.  
  64.  
  65.         return false;
  66.     }
  67.  
  68.     public boolean onKeyDown(int keyCode, KeyEvent event){
  69.          super.onKeyDown(keyCode, event, mapview);
  70.          if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
  71.                 Intent data = new Intent();
  72.                 data.putExtra("key1", sLatt);
  73.                 data.putExtra("key2", sLonn);
  74.                 setResult(RESULT_OK,data);
  75.                 finish();
  76.                 return true;
  77.          }
  78.          else return false;
  79.  
  80.     }
  81. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


ignore the commented parts, I use it for debugging.
-A-
Freshman
Freshman
 
Posts: 6
Joined: Wed Mar 09, 2011 11:47 pm

Top

Re: getting onKeyDown to work?

Postby raj_J2ME » Tue Mar 15, 2011 12:53 pm

Be a bit more specific, what is the error you are getting?
was the control entered inside the onKeyDown ()?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 5 guests