How to properly use drawMyLocation

六眼飞鱼酱① 提交于 2019-12-19 11:51:21

问题


I am trying to show the users current location with the default blue dot in android. In my maps page I also have a layout that shows different points of interest. Im having trouble figuring out what to put for some of the variables and was wondering if someone could help me out.

This is what I'm using so far to show my location.

    Location location = locationManager
                    .getLastKnownLocation(bestProvider);
            try {

                GeoPoint myPoint2 = new GeoPoint(
                        (int) (location.getLatitude() * 1E6),
                        (int) (location.getLongitude() * 1E6));
                newoverlay.drawMyLocation(null, mapView, location, myPoint2,
                        1000);
                mapOverlays.add(newoverlay);
            } catch (NullPointerException e) {
                GeoPoint myPoint2 = new GeoPoint((int) (-1 * 1E6),
                        (int) (-1 * 1E6));
                **newoverlay.drawMyLocation(null, mapView, location, myPoint2,
                        1000);**
                mapOverlays.add(newoverlay);
            }

I'm not sure what to put as the Canvas so I placed it with null so that it would compile. I'm using the location from a location Manager and I have my geopoint from the location variable. I'm also unsure what the "when" parameter is supposed to be.

I was also wondering how the blue bubble knows to move with the person, does the picture update every x milliseconds depending on the "when" parameter?

So far the app isn't crashing, but it is also not showing the blue dot at any location. I'm sure I just need help with finding what the canvas parameter should be.

Thanks


回答1:


try this way in your map activity

class CurOverlay extends Overlay {
        private GeoPoint pointToDraw;

        public void setPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }

        @Override
        public boolean draw(Canvas canvas, MapView curmapView, boolean shadow,
                long when) {
            super.draw(canvas, curmapView, shadow);

            // convert point to pixels
            Point screenPts = new Point();
            curmapView.getProjection().toPixels(pointToDraw, screenPts);

            // add marker
            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pinsource);
            canvas.drawBitmap(bmp, screenPts.x - 28, screenPts.y - 48, null);
            return true;
        }

    }

i hope this will work for you.



来源:https://stackoverflow.com/questions/7267077/how-to-properly-use-drawmylocation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!