好的,我以前问过这个问题。有一个答案。我将更深入地了解我正在做的事情。我正在为拥有多家商店的商家创建一款应用。我有一个布局,显示每个商店的按钮。点击其中一个按钮,它会带您到另一个视图,上面有两个按钮。在这个视图中有一个按钮,当你点击它时它会为你调用商店,让它工作得很好。当您单击它时,同一视图中的另一个按钮会将您带到Google地图并显示商店所在位置。一切正常。但我想要的是一个标记,以显示在地图上。我不需要它做什么,只是向客户展示这是商店所在的位置。这是我正在使用的代码--->
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.MyLocationOverlay;
import android.os.Bundle;
public class Main extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView navView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navView = (MapView) findViewById(R.id.navView);
navView.displayZoomControls(true);
navView.setBuiltInZoomControls(true);
navView.setSatellite(true);
navView.getOverlays().add(new MyLocationOverlay(this,navView));
double lat = 40.325874;
double longi = -76.002211;
GeoP = new GeoPoint((int) ( lat *1E6), (int) (longi * 1E6));
mControl = navView.getController();
mControl.animateTo(GeoP);
mControl.setZoom(20);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
答案 0 :(得分:3)
答案 1 :(得分:2)
答案 2 :(得分:1)
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue);
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext());
OverlayItem srcoverlayitem = new OverlayItem(p, "Hello!", "This is your Location.");
srcitemizedOverlay.addOverlay(srcoverlayitem);
mapView.getOverlays().clear();
mapView.getOverlays().add(srcitemizedOverlay);
mapController.animateTo(srcpoint);
mapController.setZoom(16);
在ontouch方法中使用上面的代码也使用下面的CustomItemizedOverlay.java类
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}