0

背景:-

在我的应用程序中当前正在发生的事情 - 每当我打开应用程序时,在 android 屏幕的上半部分,它会绘制一个地图,而在 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;
    }


} 

更新-

我是这样做的,但它给我抛出了 Null Pointer Exceptionlocation.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);
}
4

1 回答 1

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);
}
于 2012-10-18T04:57:16.230 回答