0

在我的地图视图中,在 SingleTapUp 中使用了 geocding。每当使用 mapview 的缩放控件时,singleTapUp 也会被调用。任何克服这种情况的建议。

我的主要方法中的代码

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


}

手势扫描方法中的代码。

  • 长按

    @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

    @覆盖

public boolean onSingleTapUp(MotionEvent arg0) { // TODO 自动生成的方法存根

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

另外使用了一个覆盖类来放置标记..

4

1 回答 1

0

由于缺少重要部分,因此调试代码有点困难,但我建议您查看以下对类似 StackOverflow 问题的答案:

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

在 MapView 上识别手势的建议解决方案是创建一个仅处理触摸事件的自定义叠加层,并将其作为第一个叠加层插入。使用这种方法,我能够创建一个小型测试应用程序,该应用程序使用了您的大部分代码,并且正确地忽略了在 MapView 上缩放手势的捏合。

于 2012-08-04T15:37:10.683 回答