2012-06-21 42 views
0

這是我寫的代碼,用於獲取移動設備的GPS座標並指向您所在的位置。但是在改進中,我需要得到1公里的半徑圓。我怎麼才能得到它?安卓半徑環繞地理位置

package m.a.p; 
public class MappingActivity extends MapActivity {  
    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); 

     overlaylist = mapV.getOverlays(); 

     d = getResources().getDrawable(R.drawable.point); 

     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 = location.getLatitude(); 
      lng = 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) { 
       TextView result = (TextView) findViewById(R.id.result); 
       result.setText("Checked the Plce"); 
      } 
     }); 
     addplace.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       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() { 
     return false; 
    } 

    public class MyLocationListener implements LocationListener { 

     public void onLocationChanged(Location location) { 
      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); 

      OverlayItem overlayItem = new OverlayItem(GeoP, "You are Here", 
        "Point"); 
      CustomPinpoint custom = new CustomPinpoint(d, MappingActivity.this); 
      custom.insertPinpoint(overlayItem); 
      overlaylist.add(custom); 
     } 

     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

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

custompinpoint 公共類CustomPinpoint擴展ItemizedOverlay {

private ArrayList<OverlayItem> pinpoints = new ArrayList <OverlayItem>(); 
    private Context c; 


    public CustomPinpoint(Drawable defaultMarker) { 
     super(boundCenter(defaultMarker)); 
    } 

    public CustomPinpoint(Drawable m, Context context){ 
     this(m); 
     c = context; 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     // TODO Auto-generated method stub 
     return pinpoints.get(i); 
    } 

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

    public void insertPinpoint(OverlayItem item){ 
     pinpoints.add(item); 
     this.populate(); 
    } 

} 

回答

4

嘗試使用自定義的MapOverlay畫出您的位置和它周圍的一圈絲毫重寫的onDraw()方法:

public class MyOwnLocationOverlay extends MyLocationOverlay{ 

    private MapView mapView; 
    private Paint circlePainter; 
    private Point screenCurrentPoint; 
    private GeoPoint geoCurrentPoint; 
    private int meters; 

    public MyOwnLocationOverlay(Context context, MapView mapView) { 
     super(context, mapView); 
     this.mapView = mapView; 
    } 

    // This method is used to get user submitted radius from our application 
    public void setMeters(int meters) { 
     this.meters = meters; 
    } 

    @Override 
    public synchronized boolean draw(Canvas canvas, MapView mapView, 
      boolean shadow, long when) { 
     // Set the painter to paint our circle. setColor = blue, setAlpha = 70 so the background 
     // can still be seen. Feel free to change these settings 
     circlePainter = new Paint(); 
     circlePainter.setAntiAlias(true); 
     circlePainter.setStrokeWidth(2.0f); 
     circlePainter.setColor(0xff6666ff); 
     circlePainter.setStyle(Style.FILL_AND_STROKE); 
     circlePainter.setAlpha(70); 

     // Get projection from the mapView. 
     Projection projection = mapView.getProjection(); 
     // Get current location 
     geoCurrentPoint = getMyLocation(); 
     screenCurrentPoint = new Point(); 
     // Project the gps coordinate to screen coordinate 
     projection.toPixels(geoCurrentPoint, screenCurrentPoint); 

     int radius = metersToRadius(geoCurrentPoint.getLatitudeE6() /1000000); 
     // draw the blue circle 
     canvas.drawCircle(screenCurrentPoint.x, screenCurrentPoint.y, radius, circlePainter); 
     return super.draw(canvas, mapView, shadow, when); 
    } 

    // hack to get more accurate radius, because the accuracy is changing as the location 
    // getting further away from the equator 
    public int metersToRadius(double latitude) { 
     return (int) (mapView.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));   
    } 
} 

看到這個link瞭解更多詳情

+0

WOW THNX ALOT MATE –

+0

只是一件小事......如何在MAP活動中調用它? –