我正在研究位置提醒项目。我想标记它被触摸的位置。我创建了 3 个类:GoogleDemo3、MapOverlay1 和 MapOverlay。MapOverlay1 类是 GoogleDemo3 的内部类。MapOverlay1 类在给定位置的地图上绘制标记。当地图在某个位置被触摸时,MapOverlay 应该返回地理点。因此,借助返回的地理点 MapOverlay1 应该绘制标记。但它不断给出未捕获的异常。以下是代码:package 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;
}
}
请帮忙。