2

谁能帮助我使用此代码实现 ontap() 事件?下面的代码将在谷歌地图中显示用户的当前位置和酒吧位置。该代码工作正常。但我的问题是我不知道如何实现 ontap() 事件。即使我的覆盖在 oncreate 包之外,是否可以实现 onTap 事件?

    import java.util.ArrayList;
    import java.util.List;
    import org.apache.http.NameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    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.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.Button;
    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.MyLocationOverlay;
    import com.google.android.maps.Overlay;
    import com.google.android.maps.OverlayItem;



    public class BarLocatorActivity extends MapActivity {
         Button map1,login1,menu1,power1;        
        // Creating JSON Parser object
        JSONNorman jNorman = new JSONNorman();


        private static String url_all_bar =
      "http://xxx.xxx.xxx.xxx/xampp/BARANDROID/get_all_bars.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_BARS = "bars";
        private static final String TAG_BARLAT = "bar_lat";
        private static final String TAG_BARLONG = "bar_long";

        // bars JSONArray
        JSONArray bars = null;


        private MapController mapController;
        private MapView mapView;
        private LocationManager locationManager;
        private MyLocationOverlay myLocationOverlay;

        public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.map); // bind the layout to the activity

        // Configure the Map
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        // enable Street view by default
        mapView.setStreetView(true);

       // mapView.setSatellite(true);

        // enable to show Traffic on map
        // mapView.setTraffic(true);
        mapController = mapView.getController();
        mapController.setZoom(15); // Zoon 1 is world view
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, new GeoUpdateHandler());

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

        myLocationOverlay.runOnFirstFix(new Runnable() {
          public void run() {

            mapView.getController().animateTo(myLocationOverlay.getMyLocation());
          }
        });
        new LoadAllBars().execute();
      }

      /* Initiating Menu XML file (menu.xml) */
      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      {
          MenuInflater menuInflater = getMenuInflater();
          menuInflater.inflate(R.layout.menu, menu);
          return true;
      }

      /**
       * Event Handling for Individual menu item selected
       * Identify single menu item by it's id
       * */
      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      {

          switch (item.getItemId())
          {

          case R.id.view:
              BarLocatorActivity.this.finish();
              Intent intent1 = new Intent(getApplicationContext(), 
                   AllBarActivity.class);
              startActivity(intent1);
              android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
              return true;

          case R.id.map1:
              BarLocatorActivity.this.finish();
              Intent intent2 = new Intent(getApplicationContext(), BarLocatorActivity.class);
              startActivity(intent2);
              android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
              return true;

          case R.id.login:
              BarLocatorActivity.this.finish();
              Intent intent3 = new Intent(getApplicationContext(), 
                  SignInActivity.class);
                  startActivity(intent3);
                  android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0); 
                return true;

          case R.id.exit:                       
                        BarLocatorActivity.this.finish();   
                        // The following makes the Android Gods frown upon me
                        android.os.Process.killProcess(android.os.Process.myPid());
                        System.exit(0);         
              return true;
          default:
              return super.onOptionsItemSelected(item);
          }
      }

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

      public class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
          int lat = (int) (location.getLatitude() * 1E6);
          int lng = (int) (location.getLongitude() * 1E6);
          GeoPoint point = new GeoPoint(lat, lng);  
          mapController.animateTo(point); // mapController.setCenter(point);

        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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



      @Override
      protected void onResume() {
        super.onResume();
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.enableCompass();
      }

      @Override
      protected void onPause() {
        super.onPause();
        myLocationOverlay.disableMyLocation();
        myLocationOverlay.disableCompass();
      }



    //bar location
      class LoadAllBars extends AsyncTask<String, String, String> {
          protected String doInBackground(String... args) {

              List<NameValuePair> params = new ArrayList<NameValuePair>();
              JSONObject json = jNorman.makeHttpRequest(url_all_bar, "GET", params);

              Log.d("All bars: ", json.toString());

              try {
                  // Checking for SUCCESS TAG
                  int success = json.getInt(TAG_SUCCESS);

                  if (success == 1) {
                      bars = json.getJSONArray(TAG_BARS);                   

                      for (int i = 0; i < bars.length(); i++) {                     
                          JSONObject c = bars.getJSONObject(i);
                          String bar_lat = c.getString(TAG_BARLAT);  
                          String bar_long = c.getString(TAG_BARLONG);  
                          double latt = Double.parseDouble(bar_lat);
                          double lngg = Double.parseDouble(bar_long);

                        GeoPoint points = new GeoPoint((int) (latt * 1E6),(int) (lngg * 
                        1E6));
                        MapOverlay mapOverlay = new MapOverlay();
                    mapOverlay.setPointToDraw(points);
                    List<Overlay> listOfOverlays = mapView.getOverlays();           
                    listOfOverlays.add(mapOverlay);
                          }                     
                  }     

              } catch (JSONException e) {
                  e.printStackTrace();
              }

              return null;
          }  
      }

      //end bar location

      //overlay

      class MapOverlay extends Overlay {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint points) {
            pointToDraw = points;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }

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

              // convert point to pixels
              Point screenPts = new Point();
              mapView.getProjection().toPixels(pointToDraw, screenPts);
              // add marker
              Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
              canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);          
              return true;
          }  
      } 

    } 

    thanks in advance:)
4

0 回答 0