add marker to map with onTouchEvent

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

add marker to map with onTouchEvent

Postby cnbishop » Tue Apr 20, 2010 1:08 am

i have successfully followed this tutorial:
http://mobiforge.com/developing/story/u ... ps-android

basically, it creates a map and adds an overlay item and returns the
address after a user presses the map. i want to be able to add
another overlay item/marker when the user presses the map. i am
having trouble figuring out where to do this. i have tried recreating
the code that adds the initial marker - but i keep getting errors. i
am not sure if it belongs in the onTouchEvent function, or if it
belongs out of the overlay class entirely. here is the draw function:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.         public boolean draw(Canvas canvas, MapView mapView,
  4.  
  5.         boolean shadow, long when)
  6.  
  7.         {
  8.  
  9.             super.draw(canvas, mapView, shadow);
  10.  
  11.  
  12.  
  13.             //---translate the GeoPoint to screen pixels---
  14.  
  15.             Point screenPts = new Point();
  16.  
  17.             mapView.getProjection().toPixels(p, screenPts);
  18.  
  19.  
  20.  
  21.             //---add the marker---
  22.  
  23.             Bitmap bmp = BitmapFactory.decodeResource(
  24.  
  25.                 getResources(), R.drawable.icon);
  26.  
  27.             canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50,
  28.  
  29. null);
  30.  
  31.             return true;
  32.  
  33.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


the first overlay item has the geopoints defined outside of the
overlay class, so maybe i need to somehow pass the geopoints when the
user touches the map back to the map activity? i am not sure how to
do that yet. any help is appreciated. if i find a solution i will
post it. thank you!!!
cnbishop
Freshman
Freshman
 
Posts: 8
Joined: Wed Feb 10, 2010 12:26 am

Top

Postby cnbishop » Sun Apr 25, 2010 10:15 pm

i was able to figure this out - i went about it in a slightly different way. also, i set it up so that my user can only add one marker:

mapactivity class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle savedInstanceState) {
  2.  
  3.         super.onCreate(savedInstanceState);
  4.  
  5.        
  6.  
  7.         //set up the map view - res/game.xml
  8.  
  9.         setContentView(R.layout.game);
  10.  
  11.        
  12.  
  13.         mapView = (MapView) findViewById(R.id.mapview);
  14.  
  15.         mapView.setBuiltInZoomControls(true);
  16.  
  17.      
  18.  
  19.         //when clicked goal will be added - see below
  20.  
  21.         mapView.setOnTouchListener(this);
  22.  
  23.        
  24.  
  25.      // cb: this gets the gps location - not working
  26.  
  27.        
  28.  
  29.         //LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  30.  
  31.         //LocationListener myLocationListener = new CurrentLocationListener();
  32.  
  33.         //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, myLocationListener);
  34.  
  35.         //Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  36.  
  37.         //double Latitude = currentLocation.getLatitude();
  38.  
  39.         //double Longitude = currentLocation.getLongitude();
  40.  
  41.  
  42.  
  43.        
  44.  
  45.        
  46.  
  47.         //set up map overlays ("views") that show images
  48.  
  49.         mapOverlays = mapView.getOverlays();
  50.  
  51.         drawable = this.getResources().getDrawable(R.drawable.icon);
  52.  
  53.         itemizedOverlay = new GameOverlay(drawable);
  54.  
  55.        
  56.  
  57.        
  58.  
  59.        
  60.  
  61.         point = new GeoPoint(37779300, -122419200);
  62.  
  63.        
  64.  
  65.         OverlayItem overlayitem = new OverlayItem(point, "", "");
  66.  
  67.  
  68.  
  69.         // map controller and zoom - zooms in at level 17 to geopoint
  70.  
  71.         MapController mc = mapView.getController();
  72.  
  73.         mc.setZoom(17);
  74.  
  75.         mc.animateTo(point);
  76.  
  77.        
  78.  
  79.         //adds the overlay
  80.  
  81.         itemizedOverlay.addOverlay(overlayitem);
  82.  
  83.         mapOverlays.add(itemizedOverlay);
  84.  
  85.  
  86.  
  87.     }
  88.  
  89.  
  90.  
  91.      @Override
  92.  
  93.      protected boolean isRouteDisplayed() {
  94.  
  95.           // TODO Auto-generated method stub
  96.  
  97.           return false;
  98.  
  99.      }
  100.  
  101.  
  102.  
  103.      public boolean onTouch(View v, MotionEvent event) {
  104.  
  105.        
  106.  
  107.         // variable so only one goal point can be set
  108.  
  109.         goalSet = goalSet +1;
  110.  
  111.        
  112.  
  113.         if(goalSet <=1) {// if a goal hasn't been set
  114.  
  115.                 if (event.getAction() == MotionEvent.ACTION_DOWN) { // if the user pushes down on the map
  116.  
  117.                        
  118.  
  119.                         //set the goal geopoint based on the x/y coordinates of the phone interface
  120.  
  121.                 GeoPoint pGoal = mapView.getProjection().fromPixels(
  122.  
  123.                         (int) event.getX(),
  124.  
  125.                         (int) event.getY());
  126.  
  127.                 Log.d("tag","test " + pGoal);
  128.  
  129.                
  130.  
  131.                 // set up a new overlay
  132.  
  133.                 mapOverlays = mapView.getOverlays();
  134.  
  135.                
  136.  
  137.                 // new drawable needed for different image/icon
  138.  
  139.                 drawableGoal = this.getResources().getDrawable(R.drawable.goal);
  140.  
  141.                 itemizedGoalOverlay = new GameOverlay(drawableGoal);
  142.  
  143.                 OverlayItem goaloverlayitem = new OverlayItem(pGoal, "", "");
  144.  
  145.                 itemizedGoalOverlay.addOverlay(goaloverlayitem);
  146.  
  147.                 mapOverlays.add(itemizedGoalOverlay);
  148.  
  149.                
  150.  
  151.                
  152.  
  153.                 // code to get the address of the geopoint pressed
  154.  
  155.                 Geocoder geoCoder = new Geocoder(
  156.  
  157.                         getBaseContext(), Locale.getDefault());
  158.  
  159.                     try {
  160.  
  161.                         List<Address> addresses = geoCoder.getFromLocation(
  162.  
  163.                             pGoal.getLatitudeE6()  / 1E6,
  164.  
  165.                             pGoal.getLongitudeE6() / 1E6, 1);
  166.  
  167.  
  168.  
  169.                         String add = "";
  170.  
  171.                         if (addresses.size() > 0)
  172.  
  173.                         {
  174.  
  175.                             for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
  176.  
  177.                                  i++)
  178.  
  179.                                add += addresses.get(0).getAddressLine(i) + "\n";
  180.  
  181.                         }
  182.  
  183.                        
  184.  
  185.                         //popup - used just for testing to view address/lat/log info
  186.  
  187.                         Toast.makeText(getBaseContext(), add + ", " + pGoal.getLatitudeE6() / 1E6 + "," + pGoal.getLongitudeE6() /1E6, Toast.LENGTH_SHORT).show();
  188.  
  189.                        
  190.  
  191.                         Log.v("tag", "test point = " + point + "goal = " +pGoal);
  192.  
  193.                        
  194.  
  195.                         //calculate the distance between the user and the goal
  196.  
  197.                         double distanceUserGoal = calculateDistance(point, pGoal);                
  198.  
  199.                         Log.v("tag", "test distance = " + calculateDistance(point, pGoal));
  200.  
  201.                     }
  202.  
  203.                     catch (IOException e) {                
  204.  
  205.                         e.printStackTrace();
  206.  
  207.                     }  
  208.  
  209.                
  210.  
  211.                 super.onTouchEvent(event);
  212.  
  213.                 return true;
  214.  
  215.                
  216.  
  217.                 }
  218.  
  219.                
  220.  
  221.                
  222.  
  223.         }
  224.  
  225.         // basically "removes" ontoucheventlistener - hack - couldn't find an alternate solution
  226.  
  227.         return false;
  228.  
  229.      }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


hope this could be helpful to others.
cnbishop
Freshman
Freshman
 
Posts: 8
Joined: Wed Feb 10, 2010 12:26 am

Tapping on Marker and displaying the transparent popup with

Postby kevindorson » Wed May 05, 2010 11:15 am

HI all,
I have followed the base example of HelloMapsView(http://opensource.csdn.net/uploads/doc/ ... pview.html)
On clicking the markers in the map, I need to have a transparent popup with the location detail as mentioned in the Activity Class(In this case, Hola, Mundo! needs to be displayed). This could be achieved using the canvas but I"m unable to figure that out on how to achieve it.

I have been stuck in this place. I have copied the the twoclesses below:
Urgent Help needed.
//HelloGoogleMaps Class
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.util.List;
  3.  
  4.  
  5.  
  6. import com.google.android.maps.GeoPoint;
  7.  
  8. import com.google.android.maps.MapActivity;
  9.  
  10. import com.google.android.maps.MapController;
  11.  
  12. import com.google.android.maps.MapView;
  13.  
  14. import com.google.android.maps.Overlay;
  15.  
  16. import com.google.android.maps.OverlayItem;
  17.  
  18.  
  19.  
  20. import android.app.Activity;
  21.  
  22. import android.graphics.drawable.Drawable;
  23.  
  24. import android.os.Bundle;
  25.  
  26.  
  27.  
  28. public class HelloGoogleMaps extends MapActivity {
  29.  
  30.      MapController mapController;
  31.  
  32.  
  33.  
  34.     /** Called when the activity is first created. */
  35.  
  36.     @Override
  37.  
  38.     public void onCreate(Bundle savedInstanceState) {
  39.  
  40.         super.onCreate(savedInstanceState);
  41.  
  42.         setContentView(R.layout.main);
  43.  
  44.         MapView mapView = (MapView) findViewById(R.id.mapview);
  45.  
  46.         mapView.setBuiltInZoomControls(true);
  47.  
  48.         mapController = mapView.getController();
  49.  
  50.         mapController.setZoom(2);
  51.  
  52.         List<Overlay> mapOverlays = mapView.getOverlays();
  53.  
  54.         Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
  55.  
  56.         HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
  57.  
  58.        
  59.  
  60.         GeoPoint point = new GeoPoint(45005000,-93228900);
  61.  
  62.         OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
  63.  
  64.         GeoPoint point2 = new GeoPoint(35410000, 139460000);
  65.  
  66.         OverlayItem overlayitem2 = new OverlayItem(point2, "wow", "this is tokyo");
  67.  
  68.         itemizedoverlay.addOverlay(overlayitem);
  69.  
  70.         itemizedoverlay.addOverlay(overlayitem2);
  71.  
  72.        
  73.  
  74.         mapOverlays.add(itemizedoverlay);
  75.  
  76.     }
  77.  
  78.    
  79.  
  80.     @Override
  81.  
  82.     protected boolean isRouteDisplayed() {
  83.  
  84.         return false;
  85.  
  86.     }
  87.  
  88. }
  89.  
  90.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //HelloItemizedOverlay Class
  3.  
  4. import java.util.ArrayList;
  5.  
  6.  
  7.  
  8. import android.app.AlertDialog;
  9.  
  10. import android.content.Context;
  11.  
  12. import android.graphics.drawable.Drawable;
  13.  
  14. import android.view.View;
  15.  
  16. import android.widget.Toast;
  17.  
  18.  
  19.  
  20. import com.google.android.maps.ItemizedOverlay;
  21.  
  22. import com.google.android.maps.OverlayItem;
  23.  
  24.  
  25.  
  26. public class HelloItemizedOverlay extends ItemizedOverlay {
  27.  
  28.      private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
  29.  
  30.      Context mContext ;
  31.  
  32.  
  33.  
  34.      public HelloItemizedOverlay(Drawable defaultMarker) {
  35.  
  36.           super(boundCenterBottom(defaultMarker));
  37.  
  38.            
  39.  
  40.           // TODO Auto-generated constructor stub
  41.  
  42.      }
  43.  
  44.      
  45.  
  46.      public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
  47.  
  48.         super(boundCenterBottom(defaultMarker));
  49.  
  50.             mContext = context;
  51.  
  52.      }
  53.  
  54.      
  55.  
  56.      @Override
  57.  
  58.      protected boolean onTap(int index) {
  59.  
  60.        OverlayItem item = mOverlays.get(index);
  61.  
  62.        /*// working below code, just replace */
  63.  
  64.          AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  65.  
  66.        dialog.setTitle(item.getTitle());
  67.  
  68.        dialog.setMessage(item.getSnippet());
  69.  
  70.        dialog.show();
  71.  
  72.        
  73.  
  74.        // I need to show another dialog opening up on tapping the above dialog. Need help here:
  75.  
  76.      
  77.  
  78.        
  79.  
  80.        
  81.  
  82.        return true;
  83.  
  84.      }
  85.  
  86.  
  87.  
  88.      @Override
  89.  
  90.      public int size() {
  91.  
  92.        return mOverlays.size();
  93.  
  94.      }
  95.  
  96.      
  97.  
  98.      @Override
  99.  
  100.      protected OverlayItem createItem(int i) {
  101.  
  102.        return mOverlays.get(i);
  103.  
  104.      }
  105.  
  106.      
  107.  
  108.      public void addOverlay(OverlayItem overlay) {
  109.  
  110.          mOverlays.add(overlay);
  111.  
  112.          populate();
  113.  
  114.      }
  115.  
  116.      
  117.  
  118.  
  119.  
  120. }
  121.  
  122.  
  123.  
  124.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4




Let me know if more information is requirred[syntax="java"]
kevindorson
Developer
Developer
 
Posts: 44
Joined: Mon Mar 29, 2010 11:51 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests