我有一个应用程序,其中一个活动显示地图,另一个类用于在地图上放置标记或任何覆盖项。
它工作得很好,但现在当用户触摸地图时,它会显示一条带有新位置的消息(这段代码是在additemizedoverlay的ontouche事件上实现的)。
相反,我想更改地图上新位置的PIN(主活动中的代码)。
如何将新的位置数据从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;
   }

最佳答案

由于MyMapActivity是AdditeMizedOverlay的上下文,因此可以使用:
((myMapactivity)context).setMarker(event)在additemizedoverlay中;这将调用公共方法setmarker(motionevent事件),该方法应在mymapactivity中实现。

09-26 09:07