1

我正在尝试在地图中显示我当前的位置以及地址。下面的代码工作正常,但不知何故我无法在我当前的位置周围绘制或指向一个圆圈。任何帮助将不胜感激。我使用的是旧 API。

这是我的主要课程。

    public class GMapsActivity extends MapActivity implements LocationListener { 

    private static final String TAG = "LocationActivity";
    private static GMapsActivity instance;
    private MapView mapView; 
    protected LocationManager locationManager;
    public Button retrieveLocationButton;
    Geocoder geocoder; 
    TextView locationText;
    Location location;
    MapController mapController;
    CountDownTimer locationtimer;

        //  private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
//  private static final long MINIMUM_TIME_BETWEEN_UPDATES = 9000; // in Milliseconds
//    MapOverlay mapOverlay = new MapOverlay();
    @Override        
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_gmaps);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.setBuiltInZoomControls(true);
        locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
        mapController = mapView.getController();  
        mapController.setZoom(13);
        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

    }
    public void showcurrentlocation(View view) {

        geocoder = new Geocoder(GMapsActivity.this);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        mapView.getOverlays().add(new CircleOverlay(this, location.getLatitude(),location.getLongitude(),325));  
        if (location != null) {     
            Log.d(TAG, location.toString());   
            this.onLocationChanged(location); //<6>  

            } 
        }



    @Override  
            public void onLocationChanged(Location location) {
            Log.d(TAG, "onLocationChanged with location " + location.toString());   
            String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),location.getLongitude(), location.getAltitude(),
                    location.getBearing());
            this.locationText.setText(text);  

                    try {     
                        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10> 
                        for (Address address : addresses) {    
                            this.locationText.append("\n" + address.getAddressLine(0));   
                            }          
                        int latitude = (int)(location.getLatitude() * 1000000);  
                        int longitude = (int)(location.getLongitude() * 1000000);  
                        GeoPoint point = new GeoPoint(latitude,longitude);    
                        mapController.animateTo(point); //<11> 


                    }
                    catch (IOException e) {  
                        Log.e("LocateMe", "Could not get Geocoder data", e);   
                        }

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

                    }

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

                    }

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


                    }


//                  @Override  protected void onResume() { 
//                  LocationListener locationListener = new LocationListener(){
//                        super.onResume(); 
//                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7> 
//                  }  
//              @Override  protected void onPause() { 
//                  super.onPause();   
//                  locationManager.removeUpdates(this); //<8>
//                      
//                  }


      @Override
        protected boolean isRouteDisplayed() {
           return false;

        }
}

下面是我的圈课。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.FloatMath;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class CircleOverlay extends Overlay {

    Context context;
    double mLat;
    double mLon;
    float mRadius;

    public CircleOverlay(Context _context, double _lat, double _lon, float radius ) {
        context = _context;
        mLat = _lat;
        mLon = _lon;
        mRadius = radius;

        }

    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow); 

        if(shadow) return; // Ignore the shadow layer

        Projection projection = mapView.getProjection();

        Point pt = new Point();

        GeoPoint geo = new GeoPoint((int) (mLat *1e6), (int)(mLon * 1e6));

        projection.toPixels(geo ,pt);
        float circleRadius = projection.metersToEquatorPixels(mRadius) * (1/ FloatMath.cos((float) Math.toRadians(mLat)));

        Paint innerCirclePaint;

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

        innerCirclePaint.setStyle(Paint.Style.FILL);

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

    }

}
4

1 回答 1

0

来自谷歌的例子:hello-mapview

每次将新的 OverlayItem 添加到 ArrayList 时,都必须为 ItemizedOverlay 调用 populate(),这将读取每个 OverlayItem 对象并准备绘制它们。

public void showcurrentlocation(View view) {

    geocoder = new Geocoder(GMapsActivity.this);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    mapView.getOverlays().add(new CircleOverlay(this, location.getLatitude(),location.getLongitude(),325));  
    if (location != null) {     
        Log.d(TAG, location.toString());   
        this.onLocationChanged(location); //<6>  
    } 
    populate();

}

但我认为您应该为此MyLocationOverlay使用 MyLocationOverlay 类

于 2013-02-03T14:45:19.133 回答