1

私はこのチュートリアルに従って、ここにリンクの説明を入力します。問題は、それmapView.setBuiltInZoomControls(true)がまったく機能しないことです...しかし、私が使用するとmapView.setClickable(true)、それは機能します。しかし、私のonTouch..メソッドは起動しません。これが完全なコードです。

package com.emazdoor.gpsmap;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class GPSMap_Activity extends MapActivity {

    private LocationManager locationManager;
    public MyLocationListener locationListener;
    private MapView mapView;
    private MapController mapcontroller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpsmap_);
        mapView = (MapView) findViewById(R.id.mapview);
        mapcontroller = mapView.getController();
        mapView.setBuiltInZoomControls(true);
//      mapView.setClickable(true);


        String coordinates[] = { "30", "71" };
        double latitude = Double.parseDouble(coordinates[0]);
        double longitude = Double.parseDouble(coordinates[1]);

        GeoPoint p = new GeoPoint((int) (latitude * 1E6),
                (int) (longitude * 1E6));

        mapcontroller.animateTo(p);
        mapcontroller.setZoom(7);
        mapView.invalidate();

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locationListener);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.my_location:
            Toast.makeText(GPSMap_Activity.this, "Moving to current Location",
                    Toast.LENGTH_LONG).show();
            locationListener.gpsCurrentLocation();
            return true;
        case R.id.mapview_setellite:
            Toast.makeText(GPSMap_Activity.this, "Satellite View",
                    Toast.LENGTH_LONG).show();
            if (mapView.isSatellite() == false)
                mapView.setSatellite(true);
            return true;
        case R.id.mapview_normal:
            Toast.makeText(GPSMap_Activity.this, "Normal View",
                    Toast.LENGTH_LONG).show();
            if (mapView.isSatellite() == true)
                mapView.setSatellite(false);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected boolean isRouteDisplayed() {

        return false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == 1) {
            GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),
                    (int) event.getY());
            Toast.makeText(GPSMap_Activity.this,
                    p.getLatitudeE6() / 1E6 + " " + p.getLongitudeE6() / 1E6,
                    Toast.LENGTH_LONG).show();
        }
        return true;
    }

    public class MyLocationListener implements LocationListener {

        private double tempLatitude, tempLongitude;


        public void onLocationChanged(Location location) {
            location.getLatitude();
            location.getLatitude();
            String text = "My current Location is:" + " Latitude= "
                    + location.getLatitude() + " Longitude= "
                    + location.getLongitude();

            Toast.makeText(GPSMap_Activity.this, text, Toast.LENGTH_LONG)
                    .show();

            if (location.getLatitude() == 71) {
                AlertDialog.Builder alertBox = new AlertDialog.Builder(
                        GPSMap_Activity.this);
                alertBox.setMessage("You have Entered in wrong place");
                alertBox.setNeutralButton("OK",
                        new DialogInterface.OnClickListener() {


                            public void onClick(DialogInterface dialog,
                                    int which) {
                            }
                        });
                alertBox.show();
            }

            tempLatitude = location.getLatitude();
            tempLongitude = location.getLongitude();

            this.gpsCurrentLocation();
        }

        public void gpsCurrentLocation() {
            String coordinates[] = { "" + tempLatitude, "" + tempLongitude };
            double latitude = Double.parseDouble(coordinates[0]);
            double longitude = Double.parseDouble(coordinates[1]);

            GeoPoint p = new GeoPoint((int) (latitude * 1E6),
                    (int) (longitude * 1E6));

            mapcontroller.animateTo(p);
            mapcontroller.setZoom(7);

            MyMapOverlay marker = new MyMapOverlay(p);
            List<Overlay> listofover = mapView.getOverlays();
            listofover.clear();
            listofover.add(marker);

            mapView.invalidate();

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }


    }

    public class MyMapOverlay extends Overlay {

        GeoPoint locationGeo = null;

        public MyMapOverlay(GeoPoint location) {
            this.locationGeo = location;
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            super.draw(canvas, mapView, shadow);

            Point screenPoint = new Point();
            mapView.getProjection().toPixels(this.locationGeo, screenPoint);

            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_delete), screenPoint.x, screenPoint.y, null);
        }

    }

}
4

1 に答える 1

1
Use Touch Event in MapOverlay class.


 public class MyMapOverlay extends com.google.android.maps.Overlay
{ 


            GeoPoint locationGeo = null; 


            public MyMapOverlay(GeoPoint location)
            { 
                this.locationGeo = location; 
            } 



            @Override 
            public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
                super.draw(canvas, mapView, shadow); 

                Point screenPoint = new Point(); 
                mapView.getProjection().toPixels(this.locationGeo, screenPoint); 

                canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), 
                        R.drawable.ic_delete), screenPoint.x, screenPoint.y, null); 
            } 



     @Override 
        public boolean onTouchEvent(MotionEvent event) { 
            if (event.getAction() == 1) { 
                GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(), 
                        (int) event.getY()); 
                Toast.makeText(GPSMap_Activity.this, 
                        p.getLatitudeE6() / 1E6 + " " + p.getLongitudeE6() / 1E6, 
                        Toast.LENGTH_LONG).show(); 
            } 
            return true; 
        } 



        } 


In Xml:


<com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="250dip"
        android:layout_alignParentLeft="true"
        android:apiKey="@string/Google_Api_key"
        android:clickable="true" >

    </com.google.android.maps.MapView>
于 2012-09-15T11:08:18.393 に答える