3

I have an app with one activity that displays map and another class that used to place marker or any overlay items on Map. It works great BUT for now when the user touch the map it shows a message with the new location (This code implemented on the ontouche event of the AddItemizedOverlay ).

I would like instead to change the pin on the map for the new location (code in main activity). How can I pass the new location data from the AddItemizedOverlay class to the main activity? How can I draw the new user location? Thanks!!!

myMapactivity.java

public class myMapactivity extends MapActivity 
{

private MapView mapView;
private MapController mapCtrl;
List<Overlay> mapOverlays;
GeoPoint mePoint;

AddItemizedOverlay itemizedOverlay;
OverlayItem overlayitem;

    public void onCreate(Bundle savedInstanceState)
    {
        ...

       mapView = (MapView) findViewById(R.id.mapview);
   mapView.setSatellite(false);
   mapView.setBuiltInZoomControls(true);

   mapOverlays = mapView.getOverlays();
   mapOverlays.clear();
   mapCtrl = mapView.getController();

       drawCurrentUserLocation();
     }

     private void drawCurrentUserLocation()
{

   // Drawable marker icon
   Drawable drawable_user = this.getResources()
            .getDrawable(R.drawable.mark_red);

   itemizedOverlay = new AddItemizedOverlay(drawable_user, this);

   overlayitem = new OverlayItem(mePoint, "Your Location","That is you!");

   itemizedOverlay.addOverlay(overlayitem);

   mapOverlays.add(itemizedOverlay);
   itemizedOverlay.populateNow();
   mapCtrl.animateTo(mePoint);
   mapCtrl.setZoom(17);
}
}

AddItemizedOverlay.java

   public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

   private Context context;

   public AddItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
   }

   public AddItemizedOverlay(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        this.context = context;
   }

   @Override
   public boolean onTouchEvent(MotionEvent event, MapView mapView)
   {   

       if (event.getAction() == 1) {
           GeoPoint geopoint = mapView.getProjection().fromPixels(
               (int) event.getX(),
               (int) event.getY());
           // latitude
           double lat = geopoint.getLatitudeE6() / 1E6;
           // longitude
           double lon = geopoint.getLongitudeE6() / 1E6;

           Geocoder geocoder = new Geocoder(context, Locale.getDefault());   
           String result = null;
           try {
               List<Address> list = geocoder.getFromLocation(lat, lon, 1);
               if (list != null && list.size() > 0) {
                   Address address = list.get(0);
                   // sending back first address line and locality
                   result = address.getAddressLine(0) + ", " + address.getLocality();
                   Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
               }
           } catch (IOException e) {
               Log.e("Error", "Impossible to connect to Geocoder", e);
           } 

       }
       return false;
   } 
Dave Martin
  • 1,387
  • 2
  • 10
  • 8

1 Answers1

0

Since myMapactivity is the context for AddItemizedOverlay, you can use:

((myMapactivity)context).setMarker(event) in AddItemizedOverlay; this will call a public method setMarker(MotionEvent event) that should be implemented in myMapactivity.

Tamir Scherzer
  • 995
  • 8
  • 8
  • Thanks Tamir. I'm newb at Android so I hope it's not a big request but can you please give me a simple example for the method at myMapActivity, or can you give me a link where I can read more about it? Thank you very much, Dave – Dave Martin Oct 15 '12 at 20:14