将GeoPoint发送到另一个Activity

时间:2012-03-27 14:37:46

标签: java android google-maps

我让用户放一个标记,如果他放另一个标记,我会删除另一个标记 然后:

我想将geoPoint作为此标记并将其作为String发送到另一个Activity 这是我的代码,

如何通过意图将markerGP = pt; markerGP发送给另一个Activity

  public class AndroidLocationActivity extends MapActivity {

private MapView map=null;
    private MyLocationOverlay me=null;
    private LocationManager locationManager;
    private GeoPoint markerGP = null;
    private boolean markerAdded = false;

     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        map=(MapView)findViewById(R.id.mapview);

        map.getController().setCenter(getPoint(40.76793169992044,
                        -73.98180484771729));
        map.getController().setZoom(17);
        map.setBuiltInZoomControls(true);

        Drawable marker=getResources().getDrawable(R.drawable.marker);

        marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                        marker.getIntrinsicHeight());

        map.getOverlays().add(new SitesOverlay(marker));

        myLocation();
     }

     @Override
     public void onResume() {
        super.onResume();

        me.enableCompass();
    }  

    @Override
     public void onPause() {
        super.onPause();
       // markerAdded = false;
        me.disableCompass();
    }  

    @Override
    protected boolean isRouteDisplayed() {
        return(false);
     }

     @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_S) {
                map.setSatellite(!map.isSatellite());
                return(true);
        }
        else if (keyCode == KeyEvent.KEYCODE_Z) {
                map.displayZoomControls(true);
                return(true);
        }

        return(super.onKeyDown(keyCode, event));
    }

    public void myLocation()
     {

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                        0, new GeoUpdateHandler());

        me = new MyLocationOverlay(this, map);
        me.enableMyLocation();
        map.getOverlays().add(me);
        map.invalidate();

        me.runOnFirstFix(new Runnable() {
                public void run() {
                        map.getController().animateTo(
                                        me.getMyLocation());
                }
        });
   }

   private GeoPoint getPoint(double lat, double lon) {
        return(new GeoPoint((int)(lat*1000000.0),
                        (int)(lon*1000000.0)));
    }

       private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
        private List<OverlayItem> items=new ArrayList<OverlayItem>();
        private Drawable marker=null;
        private OverlayItem inDrag=null;
        private ImageView dragImage=null;
        private int xDragImageOffset=0;
        private int yDragImageOffset=0;
        private int xDragTouchOffset=0;
        private int yDragTouchOffset=0;

        private boolean moved = true;

        public SitesOverlay(Drawable marker) {
                super(marker);
                this.marker=marker;

                dragImage=(ImageView)findViewById(R.id.drag);
                xDragImageOffset=dragImage.getDrawable().getIntrinsicWidth()/2;
                yDragImageOffset=dragImage.getDrawable().getIntrinsicHeight();

                populate();
        }

        @Override
        protected OverlayItem createItem(int i) {
                return(items.get(i));
        }

        @Override
        public void draw(Canvas canvas, MapView mapView,
                        boolean shadow) {
                super.draw(canvas, mapView, shadow);

                boundCenterBottom(marker);
        }

        @Override
        public int size() {
                return(items.size());
        }

          @Override
          public boolean onTouchEvent(MotionEvent event, MapView mapView) {
                final int action=event.getAction();
                final int x=(int)event.getX();
                final int y=(int)event.getY();

                GeoPoint pt=map.getProjection().fromPixels(x-xDragTouchOffset,
                                y-yDragTouchOffset);

                boolean result=false;

                if (action==MotionEvent.ACTION_DOWN) {
                        moved = false;

                        for (OverlayItem item : items) {
                                Point p=new Point(0,0);
                                inDrag = null;
                                map.getProjection().toPixels(item.getPoint(), p);

                                if (hitTest(item, marker, x-p.x, y-p.y)) {
                                        result=true;
                                        inDrag=item;
                                        items.remove(inDrag);
                                        populate();

                                        xDragTouchOffset=0;
                                        yDragTouchOffset=0;

                                        setDragImagePosition(p.x, p.y);
                                        dragImage.setVisibility(View.VISIBLE);

                                        xDragTouchOffset=x-p.x;
                                        yDragTouchOffset=y-p.y;

                                        break;
                                }
                        }
                }
                else if (action==MotionEvent.ACTION_MOVE) {
                        moved = true;
                        if(inDrag!=null)
                        {
                                setDragImagePosition(x, y);
                                result=true;
                        }

                }
                //Drop dragged marker
                else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
                        dragImage.setVisibility(View.GONE);

                        OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
                                        inDrag.getSnippet());

                        items.add(toDrop);
                        populate();

                        inDrag=null;
                        result=true;
                }
                //Add new marker to the map
                else if (action==MotionEvent.ACTION_UP && inDrag == null) {

                        if(!markerAdded && !moved)
                        {
                                //GeoPoint `pt`
                                markerGP = pt;
                                OverlayItem toDrop=new OverlayItem(pt, "TITLE", "SNIPPET");

                                items.add(toDrop);
                                populate();

                                markerAdded = true;
                                inDrag=null;
                                result=true;
                        }
                }


                return(result || super.onTouchEvent(event, mapView));
         }

         private void setDragImagePosition(int x, int y) {
                RelativeLayout.LayoutParams lp=
                        (RelativeLayout.LayoutParams)dragImage.getLayoutParams();

                lp.setMargins(x-xDragImageOffset-xDragTouchOffset,
                                y-yDragImageOffset-yDragTouchOffset, 0, 0);
                dragImage.setLayoutParams(lp);
        }
     }

     public class GeoUpdateHandler implements LocationListener {

         @Override
         public void onLocationChanged(Location location) {
                int lat = (int) (location.getLatitude() * 1E6);
                int lng = (int) (location.getLongitude() * 1E6);
                GeoPoint point = new GeoPoint(lat, lng);
                map.getController().animateTo(point); //map.getController().setCenter(point);

        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
   }
  }

你可以修复我的代码吗?我真的被困在这里!

1 个答案:

答案 0 :(得分:1)

尝试这样做:

  items = new ();
  items.add(toDrop);
  populate();

希望会有所帮助。