onTouchEvent在地图上标记

时间:2012-12-27 17:08:52

标签: android google-maps google-maps-markers ontouchevent

我正在处理位置提醒项目。我想在触摸它的位置标记。我创建了3个类:GoogleDemo3,MapOverlay1和MapOverlay。 MapOverlay1类是GoogleDemo3的内部类。 MapOverlay1类在给定位置的地图上绘制标记。在特定位置触摸地图时,MapOverlay应返回地理位置。因此,在返回的地理位置的帮助下,MapOverlay1应该绘制标记。但它一直给予未被捕获的例外。以下是代码:     包in.out.google.demo;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
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 GoogleDemo3 extends MapActivity 
{    
MapView mapView; 
MapController mc;
GeoPoint p;

class MapOverlay1 extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   

        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        //---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(
            getResources(), R.drawable.ic_launcher);            
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
        return true;
    }

}
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_demo3);

    mapView=(MapView) findViewById(R.id.MapView);

    mc = mapView.getController();



    //---Add a location marker---
    MapOverlay1 mapOverlay1 = new MapOverlay1();
    MapOverlay mapOverlay = new MapOverlay(this,mapView); 
    p=mapOverlay.geopoint();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay1);        
    listOfOverlays.add(mapOverlay);
    mc.animateTo(p);
    mc.setZoom(17); 

    mapView.invalidate();
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

我又创建了一个MapOverlay类

package in.out.google.demo;

import android.content.Context;
import android.view.MotionEvent;
import android.webkit.WebView.FindListener;
import android.widget.Toast;

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

public class MapOverlay extends Overlay{
Context c;
GeoPoint p;
MapView mapView;
public MapOverlay(Context context, MapView mapView){
    this.c=context;
    this.mapView=mapView;
    }

  public boolean onTouchEvent(MotionEvent event) 
  {   
      //---when user lifts his finger---
      MapView mapView;
      mapView=this.mapView;

      if (event.getAction() == 1) {                
          GeoPoint p = mapView.getProjection().fromPixels(
              (int) event.getX(),
              (int) event.getY());
              Toast.makeText(c, 
                  p.getLatitudeE6() / 1E6 + "," + 
                  p.getLongitudeE6() /1E6 , 
                  Toast.LENGTH_SHORT).show();
                this.p=p;
      }
        return false;              
} 
  public GeoPoint geopoint(){
      return this.p;
  }
}

请帮忙。

1 个答案:

答案 0 :(得分:0)

您现在遇到的问题来自以下结果:

p=mapOverlay.geopoint();

p在此行中获取空值,因为类pMapOverlay的值尚未初始化。

MapOverlay替换:

GeoPoint p;

通过以下方式:

GeoPoint p = new GeoPoint(nnn, mmm);

除此之外,您的代码仍然存在一些问题:

  • 每次绘制叠加层时,您都在解码BitMap。这应该在覆盖构造函数中完成。
  • 您总是从false返回onTouchEvent(),这样就无法区分地图移动和触摸以添加标记。

问候。