Custom Map View

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

Custom Map View

Postby kec6227 » Fri Jul 24, 2009 7:42 pm

Ok, here is the thing. I wanted a way to get the lat/long coords when clicking on a map so I have made a custom MapView which implements the onTouchEvent Method so I can use the MotionEvent x,y and convert it to a GeoPoint:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.content.Context;
  2.  
  3. import android.util.AttributeSet;
  4.  
  5. import android.view.MotionEvent;
  6.  
  7. import android.widget.TextView;
  8.  
  9.  
  10.  
  11. import com.google.android.maps.GeoPoint;
  12.  
  13. import com.google.android.maps.MapView;
  14.  
  15.  
  16.  
  17. public class PointSelectionView extends MapView{
  18.  
  19.  
  20.  
  21.     float[] coords = new float[2];
  22.  
  23.    
  24.  
  25.     public PointSelectionView(Context context, String apiKey) {
  26.  
  27.         super(context, apiKey);
  28.  
  29.     }
  30.  
  31.    
  32.  
  33.     public PointSelectionView(Context context, AttributeSet attrs) {
  34.  
  35.         super(context, attrs);
  36.  
  37.     }
  38.  
  39.    
  40.  
  41.     public boolean onTouchEvent(MotionEvent me) {
  42.  
  43.        
  44.  
  45. //        TextView tv = (TextView)findViewById(R.id.point);
  46.  
  47. //        tv.setText(getGeoLocation().toString());
  48.  
  49. //        
  50.  
  51. //        return super.onTouchEvent(me);
  52.  
  53.        
  54.  
  55.  
  56.  
  57.         coords[0] = me.getX();
  58.  
  59.         coords[1] = me.getY();
  60.  
  61.  
  62.  
  63.         return super.onTouchEvent(me);
  64.  
  65.     }
  66.  
  67.    
  68.  
  69.     public GeoPoint getGeoLocation() {
  70.  
  71.         GeoPoint p = null;
  72.  
  73.        
  74.  
  75.         p = getProjection().fromPixels((int)(coords[0] * 1E6), (int)(coords[1] * 1E6));
  76.  
  77.  
  78.  
  79.         return p;
  80.  
  81.     }
  82.  
  83.  
  84.  
  85. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


It looks like it should work and everything seems to be working fine. But I add an OnClick Listener in my Activity Class that calls getGeoLocation and assigns assigns it to the text view, but I am not seeing the text view change.

Here is the main Activity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.os.Bundle;
  2.  
  3. import android.view.View;
  4.  
  5. import android.view.View.OnClickListener;
  6.  
  7. import android.view.animation.Animation;
  8.  
  9. import android.view.animation.ScaleAnimation;
  10.  
  11. import android.widget.Button;
  12.  
  13. import android.widget.TextView;
  14.  
  15.  
  16.  
  17. import com.google.android.maps.GeoPoint;
  18.  
  19. import com.google.android.maps.MapActivity;
  20.  
  21.  
  22.  
  23. public class LocationsSetup extends MapActivity {
  24.  
  25.    
  26.  
  27.     private PointSelectionView map;
  28.  
  29.     GeoPoint lastPoint;
  30.  
  31.    
  32.  
  33.     public void onCreate(Bundle savedInstanceState) {
  34.  
  35.         super.onCreate(savedInstanceState);
  36.  
  37.         setContentView(R.layout.main);
  38.  
  39.         map = (PointSelectionView)findViewById(R.id.mapview);
  40.  
  41.        
  42.  
  43.         configButtons();
  44.  
  45.     }
  46.  
  47.  
  48.  
  49.     protected boolean isRouteDisplayed() {
  50.  
  51.         return false;
  52.  
  53.     }
  54.  
  55.    
  56.  
  57.     public void configButtons() {
  58.  
  59.         final Animation grow = new ScaleAnimation(
  60.  
  61.                 1.0f, 2.0f,
  62.  
  63.                 1.0f, 2.0f, 25, 25
  64.  
  65.                 );
  66.  
  67.         grow.setDuration(500);
  68.  
  69.        
  70.  
  71.         final Animation shrink = new ScaleAnimation(
  72.  
  73.                  1.0f, .5f,
  74.  
  75.                 1.0f, .5f, 25, 25
  76.  
  77.                 );
  78.  
  79.         shrink.setDuration(500);
  80.  
  81.        
  82.  
  83.        
  84.  
  85.         final Button in = (Button)findViewById(R.id.zoomin_button);
  86.  
  87.         in.setOnClickListener(new OnClickListener() {
  88.  
  89.             public void onClick(View v) {
  90.  
  91.                 map.getController().zoomIn();
  92.  
  93.                 in.startAnimation(grow);
  94.  
  95.             }
  96.  
  97.         });
  98.  
  99.        
  100.  
  101.         final Button out = (Button)findViewById(R.id.zoomout_button);
  102.  
  103.         out.setOnClickListener(new OnClickListener() {
  104.  
  105.             public void onClick(View v) {
  106.  
  107.                 map.getController().zoomOut();
  108.  
  109.                 out.startAnimation(shrink);
  110.  
  111.             }
  112.  
  113.         });
  114.  
  115.  
  116.  
  117.        
  118.  
  119.         map.setOnClickListener(new OnClickListener() {
  120.  
  121.             public void onClick(View v) {
  122.  
  123.                 lastPoint = map.getGeoLocation();
  124.  
  125.                
  126.  
  127.                 TextView tv = (TextView)findViewById(R.id.point);
  128.  
  129.                 tv.setText(lastPoint.toString());
  130.  
  131.                 tv.invalidate();
  132.  
  133.                 tv.refreshDrawableState();
  134.  
  135.             }
  136.  
  137.         });
  138.  
  139.     }
  140.  
  141. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Finally, just for completeness, I have also included my xml file. Please let me know if you have any advice as to why I am not seeing the change. Thanks!

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:id="@+id/mainlayout"
  6.  
  7.    android:orientation="vertical"
  8.  
  9.    android:layout_width="fill_parent"
  10.  
  11.    android:layout_height="fill_parent" >
  12.  
  13.  
  14.  
  15.     <com.projects.rnt.PointSelectionView
  16.  
  17.        android:id="@+id/mapview"
  18.  
  19.        android:layout_width="fill_parent"
  20.  
  21.        android:layout_height="wrap_content"
  22.  
  23.        android:clickable="true"
  24.  
  25.        android:apiKey="@string/key"
  26.  
  27.    />
  28.  
  29.    
  30.  
  31.     <Button android:id="@+id/zoomin_button"
  32.  
  33.            android:layout_width="wrap_content"
  34.  
  35.            android:layout_height="wrap_content"
  36.  
  37.            android:background="@drawable/button_background"
  38.  
  39.            android:layout_alignParentBottom="true"
  40.  
  41.            android:layout_marginLeft="10px"
  42.  
  43.            android:layout_marginRight="10px"
  44.  
  45.            android:textSize="15pt"
  46.  
  47.            android:textColor="#FFFFFF"
  48.  
  49.            android:text="+" />
  50.  
  51.  
  52.  
  53.     <Button android:id="@+id/zoomout_button"
  54.  
  55.            android:layout_width="wrap_content"
  56.  
  57.            android:layout_height="wrap_content"
  58.  
  59.            android:background="@drawable/button_background"
  60.  
  61.            android:layout_alignParentBottom="true"
  62.  
  63.            android:textSize="15pt"
  64.  
  65.            android:textColor="#FFFFFF"
  66.  
  67.            android:layout_toRightOf="@id/zoomin_button"
  68.  
  69.            android:text="-" />
  70.  
  71.            
  72.  
  73.         <TextView android:id="@+id/point"
  74.  
  75.         android:layout_alignParentBottom="true"
  76.  
  77.         android:layout_width="wrap_content"
  78.  
  79.                 android:layout_height="wrap_content"
  80.  
  81.                 android:textColor="#FFFFFF"
  82.  
  83.                 android:text="No Point Yet"
  84.  
  85.                 android:layout_toRightOf="@id/zoomout_button"
  86.  
  87.       />
  88.  
  89.            
  90.  
  91. </RelativeLayout>
Parsed in 0.006 seconds, using GeSHi 1.0.8.4




EDIT: This probably could have gone in the layout and resource section as well, but hopefully will get help here as well
kec6227
Developer
Developer
 
Posts: 30
Joined: Fri Jul 24, 2009 7:31 pm

Top

Postby kec6227 » Mon Jul 27, 2009 5:15 pm

For those interested, I have fixed this. I apparently missed the setOnTouchListener which includes a motionevent. Therefore, I was able to ditch the whole custom MapView and go to the google maps mapview; I simply implemented onTouchListener:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         map.setOnTouchListener(new OnTouchListener() {
  2.  
  3.             public boolean onTouch(View v, MotionEvent event) {
  4.  
  5.                 float x = event.getX();
  6.  
  7.                 float y = event.getY();
  8.  
  9.                
  10.  
  11.                 GeoPoint loc = map.getProjection().fromPixels((int)(x * 1E6), (int)(y * 1E6));
  12.  
  13.                
  14.  
  15.                 TextView tv = (TextView)findViewById(R.id.point);
  16.  
  17.                 tv.setText(loc.toString());
  18.  
  19.                 return true;
  20.  
  21.             }
  22.  
  23.         });
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
kec6227
Developer
Developer
 
Posts: 30
Joined: Fri Jul 24, 2009 7:31 pm

Accuracy

Postby kec6227 » Mon Jul 27, 2009 7:20 pm

Does anyone know of a more accurate way to get the GeoPoint on click? This is putting my markers all over the place (but mostly off the actual map.)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. float x = event.getX();
  2.  
  3.                 float y = event.getY();
  4.  
  5.                
  6.  
  7.                 GeoPoint loc = map.getProjection().fromPixels((int)(x * 1E6), (int)(y * 1E6));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I am hoping for something much more accurate than that as it seems very unreliable.
kec6227
Developer
Developer
 
Posts: 30
Joined: Fri Jul 24, 2009 7:31 pm

Postby kec6227 » Wed Jul 29, 2009 2:10 pm

Victory is mine!

Well I am updating this in case anyone is trying to do something similar and ends up here. Turns out when using fromPixels(int x, int y) it performs the "* 1E6" math for you, so taking that out makes it work. Here is my final method to place a marker on the map where the user clicks.

Thanks goes to this tutorial:
http://mobiforge.com/developing/story/u ... ps-android

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         map.setOnTouchListener(new OnTouchListener() {
  2.  
  3.             public boolean onTouch(View v, MotionEvent event) {
  4.  
  5.                 int x = (int)(event.getX());// * event.getXPrecision() * 1E6);
  6.  
  7.                 int y = (int)(event.getY());// * event.getYPrecision() * 1E6);
  8.  
  9.                
  10.  
  11.                 Projection proj = map.getProjection();
  12.  
  13.                 GeoPoint loc = proj.fromPixels(x, y);
  14.  
  15.                
  16.  
  17.  
  18.  
  19.                 TextView tv = (TextView)findViewById(R.id.point);
  20.  
  21.                 tv.setText(new Integer(map.getOverlays().size()).toString());
  22.  
  23.                 if(mark) {
  24.  
  25.                     addMarker(loc);
  26.  
  27.                     v.invalidate();
  28.  
  29.                 }
  30.  
  31.  
  32.  
  33.                 mark = false;
  34.  
  35.  
  36.  
  37.                 return map.onTouchEvent(event);
  38.  
  39.             }
  40.  
  41.         });
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
kec6227
Developer
Developer
 
Posts: 30
Joined: Fri Jul 24, 2009 7:31 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests