How to properly use drawMyLocation

2020-03-31 08:52发布

问题:

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.



标签: android maps