0

In my mapview, Used geocding inside SingleTapUp. Whenever zoom controls of mapview are used, singleTapUp also gets invoked. Any suggestions to overcome this scenario.

The code in my main method

 public static TextView info;
 private MapController mapController;
private MapView mapView;
public LocationManager orgManager;
private MyOverlays itemizedoverlay, blackmarker;
public boolean gps_enabled = false;
public boolean network_enabled = false;
public Criteria c;
public String bestProvider;

public double orginLongitude, orginLatitude, destLongtitude, destLatitude,
currLatitude, currLongtitude, lat, lon;
public boolean isLongPress= false, GPSfix = false;

Location originLocation = new Location("source");
Location currLocation = new Location("current");
Location destinationLocation = new Location("destination");
Location fixlocation= new Location("fix");

NotificationManager nm;
Vibrator v;
MediaPlayer mp = new MediaPlayer();

private TextToSpeech myTTS;
private int MY_DATA_CHECK_CODE = 0;
private List<Overlay> mapOverlays;
private Projection projection;

GestureDetector gestureScanner;
long lastGesture = System.currentTimeMillis();

GeoPoint markerPoint = null;
private int count = 0;
MyLocationOverlay mo;

public GeoPoint source;

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

    info = (TextView) findViewById(R.id.infopane);
    mapView = (MapView) findViewById(R.id.mapView);
    //Button myloc = (Button)findViewById(R.id.myloc);

    info.setText("Please Tap on the Map");

    orgManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);

    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(true);
    markerPoint = null;
    mapController = mapView.getController();
    mapController.setZoom(16); // Zoom 1 is world view
    mapView.invalidate();

    mo = new MyLocationOverlay(MapClass.this, mapView);
    mapView.getOverlays().add(mo);

    mapOverlays = mapView.getOverlays();
    projection = mapView.getProjection();

    Drawable drawable = this.getResources()
            .getDrawable(R.drawable.mark_red);
    Drawable black_marker = this.getResources().getDrawable(
            R.drawable.marker_black);
    itemizedoverlay = new MyOverlays(drawable);
    blackmarker = new MyOverlays(black_marker);


    try {
        gps_enabled = orgManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.i("success", "Gps:" + String.valueOf(gps_enabled)+ "N/w:" + String.valueOf(network_enabled) );
    } catch (Exception ex) {

        Log.i("sample", "sample");
    }

    if (!gps_enabled) {
        Log.i("failure", "Gps:" + String.valueOf(gps_enabled)+ "N/W:" + String.valueOf(network_enabled) );
        final AlertDialog.Builder builder = new AlertDialog.Builder(MapClass.this);
        builder.setTitle("Attention!");
        builder.setMessage("Sorry, location is not determined. Please open app after enabling GPS & wireless networks").setCancelable(false).setPositiveButton("OK", new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                finish();

            }
        });

        AlertDialog alert = builder.create();  
        alert.show();  
    }



    if(gps_enabled){

        orgManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        fixlocation = orgManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        Log.i("fix", String.valueOf(fixlocation.getLatitude() + fixlocation.getLongitude()));

        if (fixlocation!=null){
            originLocation.setLatitude(fixlocation.getLatitude());
            originLocation.setLongitude(fixlocation.getLongitude());
            Log.i("origin fix", String.valueOf(originLocation.getLatitude() + originLocation.getLongitude()));
            GPSfix = true;
        }else Toast.makeText(MapClass.this, "", Toast.LENGTH_SHORT).show(); 
    }

    if (GPSfix) {

        // double a = originLocation.getLatitude();
        // double b = originLocation.getLongitude();

        source = new GeoPoint(
                (int) ((fixlocation.getLatitude()) * 1E6),
                (int) ((fixlocation.getLongitude()) * 1E6));
        mapController.animateTo(source);
        mapView.getMapCenter();
        createMarker();
        Toast.makeText( MapClass.this," Source GPS Values Latitude: "
                + fixlocation.getLatitude() + " Longitude: "
                + fixlocation.getLongitude(), Toast.LENGTH_SHORT).show();

    }
    else Toast.makeText(MapClass.this, "Wait for GPS", Toast.LENGTH_SHORT).show();

    gestureScanner = new GestureDetector(this, this);


}

The code in gesture scanner method.

  • onLongPress

    @Override
    public void onLongPress(final MotionEvent e) {
        // TODO Auto-generated method stuMapClass Toast.makeText(MapClass.this,
        // "LongPress", Toast.LENGTH_SHORT)
    
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MapClass.this);
    
        // Setting Dialog Title
        alertDialog.setTitle("Alert!!");
    
        // Setting Dialog Message
        alertDialog.setMessage("Do you want to select this as destination");
    
        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
    
                String add = "";
    
                GeoPoint geopoint = mapView.getProjection().fromPixels(
                        (int) e.getX(), (int) e.getY());
                // latitude
                lat = geopoint.getLatitudeE6() / 1E6;
                // longitude
                lon = geopoint.getLongitudeE6() / 1E6;
    
                Geocoder geoCoder = new Geocoder(getBaseContext(),
                        Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                            geopoint.getLatitudeE6() / 1E6,
                            geopoint.getLongitudeE6() / 1E6, 1);
    
                    if (addresses.size() > 0) {
                        for (int i = 0; i < addresses.get(0)
                                .getMaxAddressLineIndex(); i++)
                            add += addresses.get(0).getAddressLine(i)
                            + "\n";
                    }
    
                    Toast.makeText(getBaseContext(), add,
                            Toast.LENGTH_SHORT).show();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
    
                mapOverlays.add(new MyOverlay((originLocation
                        .getLatitude()),
                        (originLocation.getLongitude()), lat, lon));
    
                destinationLocation.setLatitude(lat);
                destinationLocation.setLongitude(lon);
    
                String distance = Float.toString((originLocation
                        .distanceTo(destinationLocation) / 1000));
                createblack();
                count++;
    
                Toast.makeText(getBaseContext(), distance + "kms",
                        Toast.LENGTH_SHORT).show();
    
                info.setText("Selected destination is " + "\n" + add
                        + "\n" + " The journey distance is " + distance
                        + " Kms ");
    
                mapView.postInvalidate();
                isLongPress = true;
    
                mapController.animateTo(source);
                mapView.getMapCenter();
    
    
    
            }
    
    
    
            private void createblack() {
                // TODO Auto-generated method stub
                GeoPoint p = new GeoPoint((int) (lat * 1E6),
                        (int) (lon * 1E6));
                OverlayItem overlayitem = new OverlayItem(p,
                        "Destination", "");
                blackmarker.addOverlay(overlayitem);
                mapView.getOverlays().add(blackmarker);
            }
        });
        // Setting Negative "NO" Button
        alertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                Toast.makeText(getApplicationContext(),
                        "You clicked on NO", Toast.LENGTH_SHORT).show();
                dialog.cancel();
            }
        });
    
    
    
        // Showing Alert Message
        alertDialog.show();
    
    
    
    
    }
    
    • OnsingeTapup

    @Override

public boolean onSingleTapUp(MotionEvent arg0) { // TODO Auto-generated method stub

        Log.i("SingleTap", arg0.toString());

        GeoPoint geopoint = mapView.getProjection().fromPixels(
                (int) arg0.getX(), (int) arg0.getY());

        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
        try {
            List<Address> addresses = geoCoder.getFromLocation(
                    geopoint.getLatitudeE6() / 1E6,
                    geopoint.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";
            }

            if (count == 0) {

                info.setText("The location you have selected is" + "\n" + add
                        + "\n" + "To confirm it longpress the location");
            } else
                Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT)
                .show();
        } catch (IOException ioe2) {
            ioe2.printStackTrace();
        }
        return true;
    }

Additionally used a Overlay Class to place markers..

Satheesh
  • 646
  • 1
  • 10
  • 33

1 Answers1

0

It is a bit hard to debug your code as there are important parts missing but I would suggest taking a look at the following answer to a similar StackOverflow question:

https://stackoverflow.com/a/2990749/376016

The proposed solution for recognising gestures on the MapView is to create a custom Overlay that only handles touch events and insert it as the first overlay. Using this approach I was able to create a small test application that used most of your code and correctly ignored the pinch to zoom gestures on the MapView.

Community
  • 1
  • 1
Charles Harley
  • 7,184
  • 3
  • 32
  • 38