android MAP半径和标记不居中

时间:2012-06-22 18:23:38

标签: android google-maps

嘿,这就是我为了获得半径以及当前位置点所写的内容。但它没有居中和显示。我做错了什么? 如下所示 请善待我的话。谢谢

enter image description here

public class MappingActivity extends MapActivity {
    /** Called when the activity is first created. */

    MapController mControl;
    GeoPoint GeoP;
    MapView mapV;
    Drawable d;
    List<Overlay> overlaylist;

    public double lat;
    public double lng;
    Button checkin, addplace;

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
                                                                        // Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in
                                                                    // Milliseconds

    protected LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapV = (MapView) findViewById(R.id.mapview);
        checkin = (Button) findViewById(R.id.check);
        addplace = (Button) findViewById(R.id.addp);


        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());

        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {

            lat = 6;//location.getLatitude();
            lng = 77.6;//location.getLongitude();

        }

        Button check = (Button) findViewById(R.id.check);
        Button addplace = (Button) findViewById(R.id.addp);
        Button nearby = (Button) findViewById(R.id.point);

        check.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView result = (TextView) findViewById(R.id.result);
                result.setText("Checked the Plce");
            }
        });
        addplace.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView result = (TextView) findViewById(R.id.result);
                result.setText("Added the Plce");
            }
        });
        nearby.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView result = (TextView) findViewById(R.id.result);
                result.setText("Nearby the Plce");
            }
        });

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            String message = String.format("You are Here");

            Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG)
                    .show();

            GeoP = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

            mControl = mapV.getController();
            mControl.animateTo(GeoP);
            mControl.setZoom(19);

            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listOfOverlays = mapV.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);        
            mapV.invalidate();

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        class MapOverlay extends com.google.android.maps.Overlay
        {
            @Override
            public void draw(Canvas canvas, MapView mapView, boolean shadow) {

                 super.draw(canvas, mapView, shadow); 

                 Projection projection = mapView.getProjection();

                 Point pt = new Point();

                 projection.toPixels(GeoP ,pt);
                 float circleRadius = projection.metersToEquatorPixels(50);



                 Paint innerCirclePaint;

                 innerCirclePaint = new Paint();
                 innerCirclePaint.setColor(Color.BLUE);
                 innerCirclePaint.setAlpha(25);
                 innerCirclePaint.setAntiAlias(true);

                 innerCirclePaint.setStyle(Paint.Style.FILL);

                 Paint CirclePaint;
                 CirclePaint = new Paint();
                 CirclePaint.setColor(Color.BLUE);
                 CirclePaint.setAlpha(100);
                 CirclePaint.setAntiAlias(true);




               //---add the marker---
                    Bitmap bmp = BitmapFactory.decodeResource(
                        getResources(), R.drawable.point);            
                    canvas.drawBitmap(bmp, pt.x, pt.y-bmp.getHeight(), CirclePaint);              
                    super.draw(canvas,mapView,shadow);

                 canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);




            }
    }
    }
}

1 个答案:

答案 0 :(得分:0)

您的示例代码对我来说很好,但我使用它有点不同

public class Map extends MapActivity{

    private double latitude, longitude, accuracy;
    private int centerLatitude, centerLongitude;
    private MyLocationOverlay userLocationOverlay;
    private MapView mapView;
    private GeoPoint point, center;
    private MapController mapController;
    private CountDownTimer timer;
    @Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_view);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(false);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.dotpin);
    MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);

    //Open Database
    DbHelper dbHelper = new DbHelper(getApplicationContext());
    SQLiteDatabase db = dbHelper.getReadableDatabase();

    //Get Values
    String[] Labels = {DbHelper.Latitude, DbHelper.Longitude, DbHelper.Accuracy};
    Cursor cursor = db.query(DbHelper.TABLE_NAME, Labels, null, null, null, null, null);
    if(cursor.moveToFirst()){
        do{
            latitude = cursor.getDouble(cursor.getColumnIndex(Labels[0])) *1000000;
            longitude = cursor.getDouble(cursor.getColumnIndex(Labels[1])) *1000000;
            accuracy = cursor.getDouble(cursor.getColumnIndex(Labels[2]));
        }while(cursor.moveToNext());
    }

    //Close Database
    cursor.close();
    db.close();
    dbHelper.close();


    //User Location Overlay
    userLocationOverlay = new MyLocationOverlay(getApplicationContext(), mapView);

    //Set Location Overlay
    point = new GeoPoint((int)(latitude),(int)(longitude));
    OverlayItem overlayitem = new OverlayItem(point, "Radius", String.format("%.0f m", accuracy));
    itemizedoverlay.addOverlay(overlayitem);

    //Add overlays
    MapOverlay mapOverlay = new MapOverlay();
    mapOverlays.add(mapOverlay);
    mapOverlays.add(userLocationOverlay);
    mapOverlays.add(itemizedoverlay);
}

@Override
protected void onResume() {
    super.onResume();
    userLocationOverlay.enableMyLocation();

    //Pan to location
    mapController = mapView.getController();

    //Update the map view
    zoomUpdate();

    //Refresh view
    mapView.postInvalidate();
}

@Override
protected void onPause() {
    super.onPause();
    userLocationOverlay.disableMyLocation();
    if(timer!=null)
        timer.cancel();
}

protected void zoomUpdate(){
    if(timer != null)
        timer.cancel();
    timer = new CountDownTimer(5000, 1000) {
        public void onTick(long millisUntilFinished){}
        public void onFinish() {
            if(userLocationOverlay.getMyLocation() != null)
                zoomInOnPoints(point, userLocationOverlay.getMyLocation());
            else
                Toast.makeText(getApplicationContext(), "Waiting for location", Toast.LENGTH_SHORT).show();
            zoomUpdate();
        }
    };

    timer.start();
}

@SuppressWarnings("deprecation")
private void zoomInOnPoints(GeoPoint setLocation, GeoPoint userLocation) {
    int x_pixels = getWindowManager().getDefaultDisplay().getWidth();
    int y_pixels = getWindowManager().getDefaultDisplay().getHeight();
    boolean set = false;
    int latSpan, longSpan, rightEdge, leftEdge, topEdge, bottomEdge;
    boolean setIsIn, userIsIn;
    center = new GeoPoint((point.getLatitudeE6() + userLocationOverlay.getMyLocation().getLatitudeE6())/2,(point.getLongitudeE6() + point.getLongitudeE6())/2);
    centerLatitude = center.getLatitudeE6();
    centerLongitude = center.getLongitudeE6();
    mapController.animateTo(center);
    while(!set){            
        latSpan = mapView.getLatitudeSpan();
        longSpan = mapView.getLongitudeSpan();
        rightEdge = centerLongitude+longSpan/2;
        leftEdge = centerLongitude-longSpan/2;
        topEdge = centerLatitude+latSpan/2;
        bottomEdge = centerLatitude-latSpan/2;
        setIsIn = setLocation.getLatitudeE6() > bottomEdge && setLocation.getLatitudeE6() < topEdge && setLocation.getLongitudeE6() < rightEdge && setLocation.getLongitudeE6() > leftEdge;
        userIsIn = userLocation.getLatitudeE6() > bottomEdge && userLocation.getLatitudeE6() < topEdge && userLocation.getLongitudeE6() < rightEdge && userLocation.getLongitudeE6() > leftEdge;
        if(setIsIn && userIsIn && mapView.getZoomLevel() < mapView.getMaxZoomLevel()){
            mapController.zoomInFixing(x_pixels/2, y_pixels/2);
            mapController.animateTo(center);
            mapView.postInvalidate();
        }
        else{
            while(!set){
                latSpan = mapView.getLatitudeSpan();
                longSpan = mapView.getLongitudeSpan();
                rightEdge = centerLongitude+longSpan/2;
                leftEdge = centerLongitude-longSpan/2;
                topEdge = centerLatitude+latSpan/2;
                bottomEdge = centerLatitude-latSpan/2;
                setIsIn = setLocation.getLatitudeE6() > bottomEdge && setLocation.getLatitudeE6() < topEdge && setLocation.getLongitudeE6() < rightEdge && setLocation.getLongitudeE6() > leftEdge;
                userIsIn = userLocation.getLatitudeE6() > bottomEdge && userLocation.getLatitudeE6() < topEdge && userLocation.getLongitudeE6() < rightEdge && userLocation.getLongitudeE6() > leftEdge;
                if(!(setIsIn && userIsIn) && mapView.getZoomLevel() > 1){
                    mapController.zoomOutFixing(x_pixels/2, y_pixels/2);
                    mapController.animateTo(center);
                    mapView.postInvalidate();
                }
                else{
                    set = true;
                }
            }
        }
    }
}

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

    public MyItemizedOverlay(Drawable arg0, Context context) {
        super(boundCenter(arg0));
        mContext = context;
        // TODO Auto-generated constructor stub
    }

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

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return mOverlays.size();
    }

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

    @Override
    protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }
}

class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {

         super.draw(canvas, mapView, shadow); 

         Projection projection = mapView.getProjection();

         Point pt = new Point();

         projection.toPixels(point ,pt);
         float circleRadius = projection.metersToEquatorPixels((float)accuracy);


         Paint innerCirclePaint;

         innerCirclePaint = new Paint();
         innerCirclePaint.setColor(Color.BLUE);
         innerCirclePaint.setAlpha(25);
         innerCirclePaint.setAntiAlias(true);

         innerCirclePaint.setStyle(Paint.Style.FILL);

         Paint CirclePaint;
         CirclePaint = new Paint();
         CirclePaint.setColor(Color.BLUE);
         CirclePaint.setAlpha(100);
         CirclePaint.setAntiAlias(true);

         canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
    }

}

}