在这里,我使用了谷歌地图和叠加层。我使用图钉的图像指向 GeoPoint。
我想为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;
}
}