0
我正在處理位置提醒項目。我想標記它被觸摸的位置。我創建了3個類:GoogleDemo3,MapOverlay1和MapOverlay。 MapOverlay1類是GoogleDemo3的內部類。 MapOverlay1類在給定位置的地圖上繪製標記。當地圖在特定位置被觸摸時,MapOverlay應返回地理點。所以在返回的地圖MapOverlay1的幫助下應該繪製標記。但它一直給予未捕獲的異常。以下是代碼: package in.out.google.demo;onTouchEvent在地圖上標記
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;
}
}
它請幫助。
感謝您的解決方案。它消失了例外。在地圖上繪製的圖標,因爲我已初始化爲您所告知的geopoint p = new Geopoint(lat,long)。但是當觸摸地圖時,它只是不響應。觸摸事件在MapOverlay類中,它應該返回到類GoogleDemo3的main方法的geopoint p,然後使用這個地址類MapOverlay1應該在地圖上繪製圖標。觸摸事件沒有響應。 – tsogtoo