在這裏,我使用谷歌地圖和覆蓋。我使用了圖釘圖像來指向GeoPoint。OnClickListener for pushpin
我想爲推針設置一個OnClickListener
事件。當用戶觸摸該引腳時,我想烘烤一條消息。以下是代碼。
import java.util.List;
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;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.widget.Toast;
public class Googlemap1_6Activity extends MapActivity
{
/** Called when the activity is first created. */
MapView mapView;
MapController mc;
GeoPoint p;
class Mapoverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when)
{
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow, when);
Point screenPts =new Point();
mapView.getProjection().toPixels(p, screenPts);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin2);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-26, null);
return true;
}
@Override
public boolean onTap(GeoPoint pl, MapView mapView) {
// TODO Auto-generated method stub
if(pl.equals(p))
{
Toast.makeText(getBaseContext(),
"This is International Airport",
Toast.LENGTH_LONG).show();
}
return super.onTap(pl, mapView);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String lat_coordinates[] ={"27.700556"};
String lng_coordinates[] ={"85.3630"};
Double lat = Double.parseDouble(lat_coordinates[0]);
Double lng = Double.parseDouble(lng_coordinates[0]);
p = new GeoPoint(
(int) (lat*1E6),
(int) (lng*1E6));
mapView = (MapView) findViewById(R.id.mapView);
mc=mapView.getController();
mapView.setBuiltInZoomControls(true);
mc.setZoom(10);
mc.animateTo(p);
Mapoverlay MapOverlay = new Mapoverlay();
List<Overlay> listofOverlays = mapView.getOverlays();
listofOverlays.clear();
listofOverlays.add(MapOverlay);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}