2

这是我处理位置的代码。每次触摸地图上的某个地方时,我都想更改可绘制对象。虽然,我得到了位置,但drawable仍然是固定的。怎么做?

public class LocationActivity extends MapActivity{

    Bundle bundle  = new Bundle();
    MapView mapView; 
    MapController mc;
    double lat,lon;
    GeoPoint p;
    ArrayList <String> address  = new ArrayList<String>();
    List<Address> addresses;
    Location location;
    private LocationManager locationManager; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        mapView = (MapView) findViewById(R.id.mapView1);



        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

         Criteria criteria = new Criteria(); 
            //criteria.setAccuracy(Criteria.ACCURACY_FINE);
             criteria.setAltitudeRequired(false);
             criteria.setBearingRequired(false);
             criteria.setCostAllowed(true);
             String strLocationProvider = lm.getBestProvider(criteria, true);



             mc = mapView.getController();


             try {
                 location = lm.getLastKnownLocation(strLocationProvider);
                if (location != null) {
                    double lon =  (double) location.getLongitude();
                    double lat = (double) location.getLatitude();
                    //lat = 20.00;
                    // lon = 52.00;
                    p = new GeoPoint((int) (lat * 1E6),  (int) (lon * 1E6));
                    mc.animateTo(p);
                    mapView.displayZoomControls(true);

                    mc.setZoom(17);

                    MapOverlay mapOverlay = new MapOverlay();
                    List<Overlay> listOfOverlays = mapView.getOverlays();
                    listOfOverlays.clear();

                    listOfOverlays.add(mapOverlay); 


                    Geocoder gcd = new Geocoder(this, Locale.getDefault());

                    try {
                        addresses = gcd.getFromLocation(lat,lon,1);
                        if (addresses.size() > 0 && addresses != null) {

                                address.add(addresses.get(0).getFeatureName()); 
                                address.add(addresses.get(0).getLocality());
                                address.add(addresses.get(0).getAdminArea());
                                address.add(addresses.get(0).getCountryName());

                                bundle.putStringArrayList("id1", address);
                        }
                        bundle.putDouble("lat", lat);
                        bundle.putDouble("lon", lon);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }



    }
    class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
            getResources(), R.drawable.logo);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {  
                 Bundle bundle  = new Bundle();
                 ArrayList <String> address  = new ArrayList<String>();

                GeoPoint p = mapView.getProjection().fromPixels(
                        (int) event.getX(),
                        (int) event.getY());

                    Geocoder geoCoder = new Geocoder(
                        getBaseContext(), Locale.getDefault());
                    try {
                        List<Address> addresses = geoCoder.getFromLocation(
                            p.getLatitudeE6()  / 1E6, 
                            p.getLongitudeE6() / 1E6, 1);
                            addOverLay();
                        String add = "";
                        if (addresses.size() > 0) 
                        {
                            address.add(addresses.get(0).getFeatureName()); 
                            address.add(addresses.get(0).getLocality());
                            address.add(addresses.get(0).getAdminArea());
                            address.add(addresses.get(0).getCountryName());

                            bundle.putStringArrayList("id1", address);

                            for(int i = 0; i <= addresses.size();i++)
                               add += addresses.get(0).getAddressLine(i) + "\n";
                        }

                        bundle.putDouble("lat",   p.getLatitudeE6()  / 1E6);
                        bundle.putDouble("lon",  p.getLongitudeE6() / 1E6);

                        Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                    }
                    catch (IOException e) {                
                        e.printStackTrace();
                    }   
                    return true;
                }
                else                
                    return false;
            }        
    } 


    public void onClick_mapButton(View v)
        {

        Intent intent = this.getIntent();
               this.setResult(RESULT_OK, intent);

               intent.putExtras(bundle);

               finish();

        }

    public void addOverLay()
    {
               MapOverlay mapOverlay = new MapOverlay();
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();

                listOfOverlays.add(mapOverlay); 
    }
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}
4

0 回答 0