Problem whit onTouch method on a mapView

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

Problem whit onTouch method on a mapView

Postby luisfigo87 » Thu Aug 20, 2009 10:03 pm

Hi guys, I have a problem whit onTouch method.

myExampleCode:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.android;
  3.  
  4.  
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.util.Log;
  9.  
  10. import android.view.MotionEvent;
  11.  
  12. import android.view.View;
  13.  
  14.  
  15.  
  16. import com.google.android.maps.GeoPoint;
  17.  
  18. import com.google.android.maps.MapActivity;
  19.  
  20. import com.google.android.maps.MapController;
  21.  
  22. import com.google.android.maps.MapView;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. public class ProvaOnTouch extends MapActivity implements View.OnTouchListener{
  29.  
  30.    
  31.  
  32.         MapView mapView;
  33.  
  34.  
  35.  
  36.         /** Called when the activity is first created. */
  37.  
  38.     @Override
  39.  
  40.     public void onCreate(Bundle savedInstanceState) {
  41.  
  42.         super.onCreate(savedInstanceState);
  43.  
  44.         setContentView(R.layout.main);
  45.  
  46.        
  47.  
  48.         mapView = (MapView) findViewById(R.id.mapview);
  49.  
  50.         mapView.setBuiltInZoomControls(true);
  51.  
  52.        
  53.  
  54.         mapView.setOnTouchListener(this);
  55.  
  56.    
  57.  
  58.         MapController mc = mapView.getController();
  59.  
  60.         mc.setZoom(16);
  61.  
  62.         mc.animateTo(new GeoPoint((int) (45.482281 * 1E6),(int) (9.229481 * 1E6)));
  63.  
  64.     }
  65.  
  66.  
  67.  
  68.         @Override
  69.  
  70.         protected boolean isRouteDisplayed() {
  71.  
  72.                 // TODO Auto-generated method stub
  73.  
  74.                 return false;
  75.  
  76.         }
  77.  
  78.  
  79.  
  80.         public boolean onTouch(View v, MotionEvent event) {
  81.  
  82.                 Log.d("tag","hello");
  83.  
  84.                 super.onTouchEvent(event);
  85.  
  86.                 return true;
  87.  
  88.         }
  89.  
  90. }
  91.  
  92.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


This code work, but I can't move the map (if I touch or move the screen, System write in Logcat "hello" but I can't drag the map.)


if I replace the onTouch method whit this code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public boolean onTouch(View v, MotionEvent event) {
  3.  
  4.                 Log.d("tag","hello");
  5.  
  6.                 return false;
  7.  
  8.         }
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

I can move the map, but "hello" has writed only once.

SomeBody save me! (cit.) :D
Last edited by luisfigo87 on Thu Aug 20, 2009 11:14 pm, edited 1 time in total.
luisfigo87
Freshman
Freshman
 
Posts: 3
Joined: Thu Aug 20, 2009 9:36 pm

Top

Postby mad.dogg » Thu Aug 20, 2009 11:01 pm

Change this
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public boolean onTouch(View v, MotionEvent event) {
  3.  
  4. Log.d("tag","hello");
  5.  
  6. super.onTouchEvent(event);
  7.  
  8. return true;
  9.  
  10. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

like this,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public boolean onTouch(View v, MotionEvent event) {
  3.  
  4. Log.d("tag","hello");
  5.  
  6. return super.onTouchEvent(event);
  7.  
  8. }
  9.  
  10.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And you should be fine. In this suggestion you are just hooking some code before the map view process the event "normally"
So you should see the hello and still be able to drag :)
If it's boring it ain't heavy enough!
mad.dogg
Developer
Developer
 
Posts: 42
Joined: Tue May 20, 2008 2:18 pm
Location: Bulgaria

Postby luisfigo87 » Thu Aug 20, 2009 11:13 pm

Hi mad.dogg, thank you for your answer, but the code you have posted don't work.

super.onTouchEvent(event) --> return always false and the behavior of the program is the same like:

Log.d("tag","hello");
return false;

I don't believe that there are some errors in Manifest or xml..
I post them anyway:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.      package="org.android"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0">
  11.  
  12.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  13.  
  14.         <activity android:name=".ProvaOnTouch"
  15.  
  16.                  android:label="@string/app_name">
  17.  
  18.             <intent-filter>
  19.  
  20.                 <action android:name="android.intent.action.MAIN" />
  21.  
  22.                 <category android:name="android.intent.category.LAUNCHER" />
  23.  
  24.             </intent-filter>
  25.  
  26.         </activity>
  27.  
  28.                 <uses-library android:name="com.google.android.maps" />
  29.  
  30.        
  31.  
  32.     </application>
  33.  
  34.     <uses-sdk android:minSdkVersion="3" />
  35.  
  36.     <uses-permission android:name="android.permission.INTERNET" />
  37.  
  38. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


and xml:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:orientation="vertical"
  7.  
  8.         android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent" >
  11.  
  12.         <com.google.android.maps.MapView
  13.  
  14.                 android:id="@+id/mapview"
  15.  
  16.                 android:layout_width="fill_parent"
  17.  
  18.                 android:layout_height="fill_parent"
  19.  
  20.                 android:clickable="true"
  21.  
  22.                 android:apiKey="myKEY" />
  23.  
  24. </LinearLayout>
  25.  
  26.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
luisfigo87
Freshman
Freshman
 
Posts: 3
Joined: Thu Aug 20, 2009 9:36 pm

Re: Problem whit onTouch method on a mapView

Postby Sibil87 » Thu Jul 22, 2010 3:30 pm

Hey!!
I got the same issue!!

I notice that if u remove:
mapView.setBuiltInZoomControls(true);

the issue do note arise!
Looks like the Built in Zoom Controls consumes the event (except the first time) and so the event do not reach the mapView and its listener!

Also it could be that actually the event is always consumed by the zoom control and the first time it is not cause the zoom control do not have focus yet...
Sibil87
Freshman
Freshman
 
Posts: 2
Joined: Thu Jul 22, 2010 3:26 pm

Re: Problem whit onTouch method on a mapView

Postby Sibil87 » Thu Jul 22, 2010 3:46 pm

Ok so it looks like the problem was definitely in the

setBuiltInZoomControls(true);

I fixed the problem using the method getZoomControls() instead of setBuiltInZoomControls(true);

getZoomControls() is DEPRECATED and have been replaces by setBuiltInZoomControls(true);
So it looks like the have break something with this new method setBuiltInZoomControls(true);

Here is how to make work with getZoomControls() :
http://stackoverflow.com/questions/2635 ... -a-mapview
(second answer)

It is mentioned: "The real trick is to use the RelativeLayout rather than LinearLayout to position the elements, as shown in this sample layout.xml:....
The ZoomControls should now appear on a long click, without stealing the map touch events."


Please let me know if u find a way to fix this issue with setBuiltInZoomControls(true).
I feel a little dirty using a deprecated method (getZoomControls())
Sibil87
Freshman
Freshman
 
Posts: 2
Joined: Thu Jul 22, 2010 3:26 pm

Re: Problem whit onTouch method on a mapView

Postby pranav09 » Sat Aug 07, 2010 7:25 am

For the more specification see the LogCat.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 5 guests