应用程序加载后在地图上绘制圆圈

时间:2012-10-18 04:36:28

标签: java android android-mapview

背景: -

在我的应用程序中当前正在发生的事情 - 每当我打开应用程序时,在Android屏幕的上半部分,它绘制一个Map,在android屏幕的下半部分显示列表视图。然后,一旦位置发生变化,它就会绘制一个圆圈,其中当前位置为圆圈的中心,并在当前位置(圆心)显示图像。一切都很好,直到这里 -

问题陈述: - 我想要的是当用户打开我的应用程序时,圈子应该立即在谷歌地图上绘制(这当前没有发生,它只在更改的位置绘制圆圈),而不等待位置变更和在圆心没有任何图像,然后如果位置发生变化,将当前位置作为圆心,并在圆心绘制圆形图像。

这是我的下面代码,它完成了我在后台提到的场景 - 如何让这段代码按照我想要的方式工作?希望我在我的问题中足够清楚。任何建议将不胜感激。

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

    mapView = (MapView) findViewById(R.id.mapView);
    listView = (ListView) findViewById(R.id.mylist);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
    locationListener = new GPSLocationListener(mapView);
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            35000, 
            10, 
            locationListener);

    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);
    mapController = mapView.getController();
    mapController.setZoom(14);
}

位置更新类,我将请求发送到Overlay以绘制圆圈

    private class GPSLocationListener implements LocationListener {

    MapOverlay mapOverlay;

    public GPSLocationListener(MapView mapView) {

    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));

            mapController.animateTo(point);
            mapController.setZoom(15);

            if (mapOverlay == null) {
                mapOverlay = new MapOverlay(this,android.R.drawable.star_on);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.add(mapOverlay);
            }
            mapOverlay.setPointToDraw(point);
            mapView.invalidate();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

绘制圆圈的类。

    class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames=new int[6];
    private Point mScreenPoints;
    private Bitmap mBitmap;
    private Paint mCirclePaint;


    public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
        imageNames[0]=currentUser;
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(0x30000000);
        mCirclePaint.setStyle(Style.FILL_AND_STROKE);
        mBitmap = BitmapFactory.decodeResource(getResources(),imageNames[0]);
        mScreenPoints = new Point();
    }

    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);
        if (pointToDraw == null) {
            return true;
        }
        mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);
        int totalCircle=5;
        int radius=40;
        int centerimagesize=35;
        for (int i = 1; i <= totalCircle; i ++) { 
            canvas.drawCircle(mScreenPoints.x,mScreenPoints.y, i*radius, mCirclePaint); 
        } 
        canvas.drawBitmap(mBitmap, (mScreenPoints.x-(centerimagesize/2)),(mScreenPoints.y-(centerimagesize/2)), null);
        super.draw(canvas,mapView,shadow);
        return true;
    }


} 

更新 -

我是这样做的,但它在location.setLatitude上给我一个空指针异常。任何人都可以帮我解释为什么它会让我NPE吗?

private Location location;

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

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

    locationListener = new GPSLocationListener();

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);
    mapView = (MapView) findViewById(R.id.mapView);
    listView = (ListView) findViewById(R.id.mylist);
    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(15);

    GeoPoint initialPoint = new GeoPoint( (int) (36.778261* 1E6), (int) (-119.417932 * 1E6));
    double latitude = initialPoint .getLatitudeE6() / 1E6;
    double longitude = initialPoint .getLongitudeE6() / 1E6;

     // Null Pointer Exception right here.
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    locationListener.onLocationChanged(location);
}

1 个答案:

答案 0 :(得分:1)

在活动locationListener.onLocationChanged(your_initial_location)

结束时致电onCreate();

否则,它正在等待第一次有效更新以运行那里设置绘图点的代码。

添加了:

onCreate()中的

....

mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(14);

GoePoint initialPoint = new GeoPoint(some initial point);
double latitude = initialPoint .getLatitudeE6() / 1E6;
double longitude = initialPoint .getLongitudeE6() / 1E6;

Location location = new Location("initialLocation");
location.setLatitude(latitude);
location.setLongitude(longitude)
locationListener.onLocationChanged(location);
}