Error while applying overlay on a location on a Google map in Android

Posted by Hiccup on Stack Overflow See other posts from Stack Overflow or by Hiccup
Published on 2012-04-05T12:10:27Z Indexed on 2012/04/05 17:29 UTC
Read the original article Hit count: 289

Filed under:
|
|
|

This is my Activity for getting Location:

public class LocationActivity extends MapActivity{

    Bundle bundle  = new Bundle();
    MapView mapView; 
    MapController mc;
    GeoPoint p;
    ArrayList <String> address  = new ArrayList<String>();
    List<Address> addresses;
    private LocationManager locationManager; 
    double lat, lng;
    /** 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);

        mapView.displayZoomControls(true);

        mc = mapView.getController();

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


            //Location location = lm.getLastKnownLocation(strLocationProvider);
             Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);



             lat = (double) location.getLatitude();
             lng = (double) location.getLongitude();

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        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,lng,1);
        if (addresses.size() > 0 && addresses != null) {

                    address.add(addresses.get(0).getFeatureName()); 

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

                    bundle.putStringArrayList("id1", address);
        }
        bundle.putDouble("lat", lat);
        bundle.putDouble("lon", lng);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        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();

                            MapOverlay mapOverlay = new MapOverlay();
                            Bitmap bmp = BitmapFactory.decodeResource(
                                    getResources(), R.drawable.crumbs_logo);   


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

                            listOfOverlays.add(mapOverlay); 
                        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;
    }

    public void FindLocation() {
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
               // updateLocation(location);

                Toast.makeText(
                        LocationActivity.this,
                        String.valueOf(lat) + "\n"
                                + String.valueOf(lng), 5000)
                        .show();

                }

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

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        };
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


    }





}

I face two problems here. One is that when I click (do a tap) on any location, the overlay is not changing to that place. Also, the app crashes when I am on the MapView page and I click on back button. What might be the error?

© Stack Overflow or respective owner

Related posts about android

Related posts about google-maps