标记未在地图上显示

时间:2012-08-20 14:07:20

标签: android google-maps android-mapview

我使用以下代码在用户的当前位置显示标记。但标记从不显示,代码显示正确的当前位置。

    mapview.setClickable(true);
    mapview.setBuiltInZoomControls(true);
    mapview.setSatellite(false);
    mapControl = mapview.getController();
    mapControl.setZoom(12);

    myLocationOverlay = new MyLocationOverlay(this, mapview);
    mapview.getOverlays().add(myLocationOverlay);

    LocationResult locationResult = new LocationResult() {
        @Override
        public void gotLocation(Location location) {

            List<Overlay> overlays = mapview.getOverlays();
            overlays.clear();

            latE6 = (int) (location.getLatitude() * 1e6);
            lonE6 = (int) (location.getLongitude() * 1e6);

            String a = String.valueOf(latE6);
            String b = String.valueOf(lonE6);

            gp = new GeoPoint(latE6, lonE6);
            mapControl.setZoom(12);
            mapControl.animateTo(gp);

            MapOverlay mapOverlay = new MapOverlay(getResources()
                    .getDrawable(R.drawable.pingreen));
            OverlayItem overlayItem = new OverlayItem(new GeoPoint(latE6,
                    lonE6), a, b);
            mapOverlay.addOverlay(overlayItem);
            overlays.add(mapOverlay);
            mapview.invalidate();
        }
    };
    MyLocation myLocation = new MyLocation();
    myLocation.getLocation(this, locationResult);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

@Override
protected void onResume() {
    myLocationOverlay.enableCompass();
    myLocationOverlay.enableMyLocation();
    super.onResume();
}

@Override
protected void onPause() {
    myLocationOverlay.disableCompass();
    myLocationOverlay.disableMyLocation();
    super.onPause();
}

public class MapOverlay extends ItemizedOverlay<OverlayItem> {
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    public MapOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = mOverlays.get(index);

        AlertDialog.Builder dialog = new AlertDialog.Builder(
                PickUpLocation.this);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());

        dialog.show();

        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        if (event.getAction() == 1) {
            GeoPoint p = mapView.getProjection().fromPixels(
                    (int) event.getX(), (int) event.getY());
            Toast.makeText(
                    getBaseContext(),
                    p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6()
                            / 1E6, Toast.LENGTH_SHORT).show();
            mapView.getOverlays().add(new MarkerOverlay(p));
            mapView.invalidate();
        }
        return false;
    }
}

class MarkerOverlay extends Overlay {
    private GeoPoint p;

    public MarkerOverlay(GeoPoint p) {
        this.p = p;
    }

    @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.pingreen);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);

        return true;
    }
}

请帮我解决这个问题。 提前致谢

1 个答案:

答案 0 :(得分:0)

在模拟器上,您可以使用DDMS发送模拟位置。否则,请使用真实设备。