2011-06-13 9 views
8

Ok ... tengo una actividad simple que da la posición GPS del usuario. lo que quiero hacer es dibujar un círculo alrededor de la posición gps. El círculo da una idea sobre la precisión del posicionamiento GPS. Por ejemplo, si la precisión es de 6 metros, el radio del círculo cubrirá 6 metros. siguiente es el código. Gracias de antemano por cualquier ayuda:android gps posición precisión círculo

import java.util.List; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
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; 

public class ShowMap extends MapActivity { 

    private MapController mapController; 
    private MapView mapView; 
    private LocationManager locationManager; 
    private GeoUpdateHandler geoUpdateHandler; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.gps); 
      mapView = (MapView) findViewById(R.id.mapview); 
      mapView.setBuiltInZoomControls(true); 
      mapView.setSatellite(true); 
      mapController = mapView.getController(); 
      mapController.setZoom(14); // Zoom 1 is world view 
      geoUpdateHandler = new GeoUpdateHandler(); 

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
       createGpsDisabledAlert(); 
      } else { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
         0,geoUpdateHandler); 

      } 

     } 
    private void createGpsDisabledAlert() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder 
        .setMessage(
          "Your GPS is disabled! Would you like to enable it?") 
        .setCancelable(false).setPositiveButton("Enable GPS", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            showGpsOptions(); 
           } 
          }); 
      builder.setNegativeButton("Do nothing", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 

    private void showGpsOptions() { 
      Intent gpsOptionsIntent = new Intent(
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(gpsOptionsIntent); 
     } 


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


    public class GeoUpdateHandler implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      int lat = (int) (location.getLatitude() * 1E6); 
      int lng = (int) (location.getLongitude() * 1E6); 
      GeoPoint point = new GeoPoint(lat, lng); 
      mapController.animateTo(point); // mapController.setCenter(point); 
      MapOverlay mapOverlay = new MapOverlay(); 
       mapOverlay.setPointToDraw(point); 
       List<Overlay> listOfOverlays = mapView.getOverlays(); 
       listOfOverlays.clear(); 
       listOfOverlays.add(mapOverlay); 

     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 


    class MapOverlay extends Overlay 
    { 
     private GeoPoint pointToDraw; 

     public void setPointToDraw(GeoPoint point) { 
     pointToDraw = point; 
     } 

     public GeoPoint getPointToDraw() { 
     return pointToDraw; 
     } 

     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 
     super.draw(canvas, mapView, shadow);   

     // convert point to pixels 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(pointToDraw, screenPts); 

     // add marker 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pen); 
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 128, null);  
     return true; 
     } 




    } 
    @Override 
    public void onPause() { 
     super.onPause(); 
     locationManager.removeUpdates(geoUpdateHandler); 
    } 

} 

Respuesta

5

Debe crear sus objetos pintura de antemano, sorteo se ejecuta mucho, por lo que mantener la asignación al mínimo.

 @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 
    super.draw(canvas, mapView, shadow);   

    // convert point to pixels 
    Point screenPts = new Point(); 
    mapView.getProjection().toPixels(pointToDraw, screenPts); 

    // add marker 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pen); 
    canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 128, null);  
    return true; 

    mPaintBorder = new Paint(); 
    mPaintBorder.setStyle(Paint.Style.STROKE); 
    mPaintBorder.setAntiAlias(true); 
    mPaintBorder.setColor(0xee4D2EFF); 
    mPaintFill = new Paint(); 
    mPaintFill.setStyle(Paint.Style.FILL); 
    mPaintFill.setColor(0x154D2EFF); 

    double lat = pointToDraw.getLatitudeE6(); 
    int radius = (int) mapView.getProjection().metersToEquatorPixels(theGpsAccuracy); 
    /** Draw the boundary of the circle */ 
    canvas.drawCircle(ScreenPts.x, ScreenPts.y, radius, mPaintBorder); 
    /** Fill the circle with semitransparent color */ 
    canvas.drawCircle(ScreenPts.x, ScreenPts.y, radius, mPaintFill); 
    } 
+1

¿Cómo se obtiene el valor de theGpsAccuracy? – Arci

+1

Cuando el oyente del GPS devuelve un objeto de ubicación, tiene un campo para eso. Consulte http://developer.android.com/reference/android/location/Location.html – dzsonni

7

Se podría también, si su trabajo con un objeto GoogleMap, añadir un círculo para representar la precisión de esta manera.

  CircleOptions co = new CircleOptions(); 
      co.center(latlngpoint); 
      co.radius(accuracy_in_meters); 
      co.fillColor(fillColor); 
      co.strokeColor(strokeColor); 
      co.strokeWidth(4.0f); 
     map.addCircle(co); 
+0

Hola, ¿cómo lo eliminas? –

+2

método addCircle() devuelve objeto Circle, que tiene el método remove() https://developers.google.com/android/reference/com/google/android/gms/maps/model/Circle.html#remove() @JawadLeWywadi – snayde

+0

Gracias me lo imagino. –