为什么Zoom无法使用Google地图?

时间:2012-09-15 10:15:03

标签: android google-maps

我正在关注本教程enter link description here。问题是mapView.setBuiltInZoomControls(true)根本不起作用......但是当我使用mapView.setClickable(true)时它就可以工作了。 BUt然后我的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);
        }

    }

}

1 个答案:

答案 0 :(得分: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>