【问题标题】:Draw Circle on the Map once the application gets loaded加载应用程序后在地图上绘制圆圈
【发布时间】:2012-10-19 08:39:55
【问题描述】:

背景:-

在我的应用程序中当前正在发生什么 - 每当我打开应用程序时,在 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;
    }


} 

更新-

我是这样做的,但它在 location.setLatitude 上向我抛出 Null Pointer Exception

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);
}

【问题讨论】:

    标签: java android android-mapview


    【解决方案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);
    }
    

    【讨论】:

    • 感谢塔米尔的建议。我怎样才能做到这一点?你能从我的代码中告诉我吗?但这会在应用程序加载后绘制圆圈?
    • 感谢您的代码。在你的代码中这个位置来自哪里?我在这里有点困惑。
    • “我想要的是当用户打开我的应用程序时,应该立即在 Google 地图上绘制圆圈...”您希望在哪里绘制?
    • 我猜是谷歌地图打开时的默认位置。是否有任何默认位置总是首先打开谷歌地图。
    • myMapView.getMapCenter();会给你它的中心。我建议您阅读以下内容:android-er.blogspot.co.il/2009/12/…
    猜你喜欢
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多