Android MapActivity& ItemizedOverlay更改onTouchEvent上的当前位置

时间:2012-10-13 21:40:25

标签: android itemizedoverlay mapactivity

我有一个应用程序,其中一个活动显示地图,另一个类用于在地图上放置标记或任何叠加项目。 它工作得很好但是现在当用户触摸地图时它会显示带有新位置的消息(此代码在AddItemizedOverlay的ontouche事件上实现)。

我想改为在地图上为新位置更改图钉(主要活动中的代码)。 如何将新的位置数据从AddItemizedOverlay类传递给主要活动?如何绘制新的用户位置? 感谢!!!

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;
   } 

1 个答案:

答案 0 :(得分:0)

由于myMapactivity是AddItemizedOverlay的上下文,因此您可以使用:

AddItemizedOverlay中的

((myMapactivity)context).setMarker(event);这将调用应在myMapactivity中实现的公共方法setMarker(MotionEvent事件)。