0

I have this textbox in the other activity and a button which leads to this map. What i want to do is that it allows the user to click the map and pin point the location they wants, and they can also fetch that location's address back to the textbox without typing. Can it be done so? Since now there is this alert dialog box which pop up after user pin point it with 2 buttons(Yes and No) So if yes, fetch that location address back to the textbox. Any idea?

    static EditText txtLocation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mc = mapView.getController();

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

        mapOverlays.add(mapOverlay);

        // obtain gps location
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationListener = new MyLocationListener();

        lm.requestLocationUpdates(
        // LocationManager.GPS_PROVIDER,
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location loc) {
            if (loc != null) {
                Toast.makeText(
                        getBaseContext(),
                        "Location changed: Lat: " + loc.getLatitude()
                                + " Lng: " + loc.getLongitude(),
                        Toast.LENGTH_SHORT).show();
            }
            p = new GeoPoint((int) (loc.getLatitude() * 1E6),
                    (int) (loc.getLongitude() * 1E6));
            mc.animateTo(p);
            mc.setZoom(18);

            // Add a location marker
            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listofOverlays = mapView.getOverlays();
            listofOverlays.clear();
            listofOverlays.add(mapOverlay);

            // invalidate() method forces the MapView to be redrawn
            mapView.invalidate();
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

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

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

    class MapOverlay extends com.google.android.maps.Overlay {

        @Override
        public boolean onTap(final GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub

            k = p;
            mc = mapView.getController();
            mc.animateTo(p);

            mapView.invalidate();

            Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
            try{
            List<Address> addresses = geoCoder.getFromLocation(
            p.getLatitudeE6() /1E6,
            p.getLongitudeE6() / 1E6, 1
            );
            String add = "";
            if (addresses.size()>0)
            {
                for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                    add += addresses.get(0).getAddressLine(i) + "\n";
            }

//          txtLocation.setText(add);   
            Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
            }
            catch (IOException e){
                e.printStackTrace();
            }

            new AlertDialog.Builder(MapsActivity.this)
                    .setTitle("Change location..")
                    .setMessage("go to the new location?")
                    .setNegativeButton("NO",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    dialog.dismiss();
                                }
                            })
                    .setPositiveButton("YES",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub
                                    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                                    try{
                                    List<Address> addresses = geoCoder.getFromLocation(
                                    p.getLatitudeE6() /1E6,
                                    p.getLongitudeE6() / 1E6, 1
                                    );
                                    String add = "";
                                    if (addresses.size()>0)
                                    {
                                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                                            add += addresses.get(0).getAddressLine(i) + "\n";
                                    }

                                    txtLocation.setText(add);   
//                                  Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                                    }
                                    catch (IOException e){
                                        e.printStackTrace();
                                    }
                                }
                            }).show();
            return true;
        }

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);
            if (k != null) {
                // ---translate the GeoPoint to screen pixels---
                Point screenPts = new Point();
                mapView.getProjection().toPixels(k, screenPts);

                // ---add the marker---
                Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                        R.drawable.marker);
                canvas.drawBitmap(bmp, screenPts.x - 10, screenPts.y - 34, null);
            }
            return true;
        }
    }
}
sowhat
  • 125
  • 1
  • 2
  • 11
  • please be clear in ur question...u want to fetch address of touched location or current location??? remove the commented code in onTap, u shud pass those lat,long values for reverse geocoding... – Pratik Bhat Nov 23 '11 at 05:22
  • I want to fetch the address to the textbox of the location that the user touched on. – sowhat Nov 23 '11 at 08:25
  • use textbox.setText(add) in onTap function – Pratik Bhat Nov 23 '11 at 09:36
  • i used it under the button Yes. And when i run it, it step into that particular sentence "txtLocation.setText(add)" How? – sowhat Nov 23 '11 at 11:10
  • txtLocation reads null. txtLocation is in another activity. Could this be the problem? – sowhat Nov 23 '11 at 11:16
  • yes u will need to use startActivityForResult() in first activity....to start this map activity, store this address as an intent extra, then in the first activity, in onactivityResult() u have to extract this extrta, and then display it in ur textbox...just brush up reading some developer docs..cheers! – Pratik Bhat Nov 23 '11 at 11:22
  • Yes i did use startActivityForResult to start the map activity. Right now i'm facing this difficult on how to fetch this addres back to the first activity textbox. – sowhat Nov 23 '11 at 11:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5270/discussion-between-user1058153-and-android-hungry) – sowhat Nov 23 '11 at 11:52

1 Answers1

1

Well you can try reverse geocoding. What that needs is you to supply the lat long of the point that the user tapped on, which is fairly easy.
Check this similar question. might help.

Community
  • 1
  • 1
Urban
  • 2,211
  • 23
  • 52
  • I tried reverse geocoding, which i placed the codes after the button Yes. – sowhat Nov 22 '11 at 14:44
  • after i click on the yes. it highlight this particular line: .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); – sowhat Nov 22 '11 at 14:48
  • i really cant understand. What highlights that part of the code and if ur app force closes, then what error has logcat given you? If there is no error, then it shud be be fetching the address. Also, try out this tutorial, explained in detail: http://www.smnirven.com/?p=39 – Urban Nov 22 '11 at 15:44
  • the textbox that i want the address to fetch back in belongs to Activity A, and this map is Activity B. – sowhat Nov 23 '11 at 11:46
  • then create a method in Activity A that given a string argument, sets the textbox to that text, and call this method from Activity B. Like in A: public void setAddressText(String Address){ MyTextView.setText(Address);} and in B: ActivityA.setAddressText(AddressFromMap); – Urban Nov 23 '11 at 12:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5273/discussion-between-user1058153-and-urban) – sowhat Nov 23 '11 at 12:23
  • it can be done in this way too. > ActivityA.textbox.setText(AddressMap); – sowhat Nov 23 '11 at 13:09