【问题标题】:Draw image on osm map osmdroid在 osm map osmdroid 上绘制图像
【发布时间】:2018-02-14 18:36:59
【问题描述】:

我想在 osm 地图上绘制图像,使用 osmdroid/osmbonuspack。我已经尝试过 Marker 和 SimpleLocationOverlay。这些将图像显示为覆盖,就像一个不会改变比例的图钉。但我想显示成为地图一部分的图像,这样当地图放大时,图像应该放大,当缩小时,它应该缩小。

【问题讨论】:

    标签: android image osmdroid osmbonuspack


    【解决方案1】:

    对GroundOverlay.java 和MainActivity.java 的osmbonuspack repo 进行一些修改:

    public class MainActivity extends AppCompatActivity {
    
        MapView mMapView = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Context ctx = getApplicationContext();
            Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
    
            setContentView(R.layout.activity_main);
    
            mMapView = (MapView) findViewById(R.id.map);
            mMapView.setTileSource(TileSourceFactory.MAPNIK);
    
            mMapView.setBuiltInZoomControls(true);
            mMapView.setMultiTouchControls(true);
            GeoPoint overlayCenterPoint = new GeoPoint(50.450667, 30.523193);
            IMapController mapController = mMapView.getController();
            mapController.setZoom(17f);
            mapController.setCenter(overlayCenterPoint);
            mMapView.setMapOrientation(0.0f);
    
            GroundOverlay myGroundOverlay = new GroundOverlay();
            myGroundOverlay.setPosition(overlayCenterPoint);
            Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher, null);
            myGroundOverlay.setImage(d.mutate());
            myGroundOverlay.setDimensions(200.0f);
            myGroundOverlay.setTransparency(0.25f);
            myGroundOverlay.setBearing(0);
            mMapView.getOverlays().add(myGroundOverlay);
    
            mMapView.invalidate();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mMapView.onResume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            mMapView.onPause();
        }
    
        public class GroundOverlay extends Overlay {
    
            protected Drawable mImage;
            protected GeoPoint mPosition;
            protected float mBearing;
            protected float mWidth, mHeight;
            protected float mTransparency;
            public final static float NO_DIMENSION = -1.0f;
            protected Point mPositionPixels, mSouthEastPixels;
    
            public GroundOverlay() {
                super();
                mWidth = 10.0f;
                mHeight = NO_DIMENSION;
                mBearing = 0.0f;
                mTransparency = 0.0f;
                mPositionPixels = new Point();
                mSouthEastPixels = new Point();
            }
    
            public void setImage(Drawable image){
                mImage = image;
            }
    
            public Drawable getImage(){
                return mImage;
            }
    
            public GeoPoint getPosition(){
                return mPosition.clone();
            }
    
            public void setPosition(GeoPoint position){
                mPosition = position.clone();
            }
    
            public float getBearing(){
                return mBearing;
            }
    
            public void setBearing(float bearing){
                mBearing = bearing;
            }
    
            public void setDimensions(float width){
                mWidth = width;
                mHeight = NO_DIMENSION;
            }
    
            public void setDimensions(float width, float height){
                mWidth = width;
                mHeight = height;
            }
    
            public float getHeight(){
                return mHeight;
            }
    
            public float getWidth(){
                return mWidth;
            }
    
            public void setTransparency(float transparency){
                mTransparency = transparency;
            }
    
            public float getTransparency(){
                return mTransparency;
            }
    
            protected void computeHeight(){
                if (mHeight == NO_DIMENSION && mImage != null){
                    mHeight = mWidth * mImage.getIntrinsicHeight() / mImage.getIntrinsicWidth();
                }
            }
    
            /** @return the bounding box, ignoring the bearing of the GroundOverlay (similar to Google Maps API) */
            public BoundingBox getBoundingBox(){
                computeHeight();
                GeoPoint pEast = mPosition.destinationPoint(mWidth, 90.0f);
                GeoPoint pSouthEast = pEast.destinationPoint(mHeight, -180.0f);
                double north = mPosition.getLatitude()*2 - pSouthEast.getLatitude();
                double west = mPosition.getLongitude()*2 - pEast.getLongitude();
                return new BoundingBox(north, pEast.getLongitude(), pSouthEast.getLatitude(), west);
            }
    
            public void setPositionFromBounds(BoundingBox bb){
                mPosition = bb.getCenterWithDateLine();
                GeoPoint pEast = new GeoPoint(mPosition.getLatitude(), bb.getLonEast());
                GeoPoint pWest = new GeoPoint(mPosition.getLatitude(), bb.getLonWest());
                mWidth = (float)pEast.distanceToAsDouble(pWest);
                GeoPoint pSouth = new GeoPoint(bb.getLatSouth(), mPosition.getLongitude());
                GeoPoint pNorth = new GeoPoint(bb.getLatNorth(), mPosition.getLongitude());
                mHeight = (float)pSouth.distanceToAsDouble(pNorth);
            }
    
            @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) {
                if (shadow)
                    return;
                if (mImage == null)
                    return;
    
                computeHeight();
    
                final Projection pj = mapView.getProjection();
    
                pj.toPixels(mPosition, mPositionPixels);
                GeoPoint pEast = mPosition.destinationPoint(mWidth/2, 90.0f);
                GeoPoint pSouthEast = pEast.destinationPoint(mHeight/2, -180.0f);
                pj.toPixels(pSouthEast, mSouthEastPixels);
                int hWidth = mSouthEastPixels.x-mPositionPixels.x;
                int hHeight = mSouthEastPixels.y-mPositionPixels.y;
                mImage.setBounds(-hWidth, -hHeight, hWidth, hHeight);
    
                mImage.setAlpha(255-(int)(mTransparency*255));
    
                drawAt(canvas, mImage, mPositionPixels.x, mPositionPixels.y, false, -mBearing);
            }
    
        }
    }
    

    你可以得到类似的东西:

    主要部分是:

    // overlay center point
    GeoPoint overlayCenterPoint = new GeoPoint(50.450667, 30.523193);
    IMapController mapController = mMapView.getController();
    mapController.setZoom(17f);
    mapController.setCenter(overlayCenterPoint);
    mMapView.setMapOrientation(0.0f);
    
    GroundOverlay myGroundOverlay = new GroundOverlay();
    myGroundOverlay.setPosition(overlayCenterPoint);
    Drawable d = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher, null);
    myGroundOverlay.setImage(d.mutate());
    
    // overlay width in meters (height calculated automatically) also you can set both width and height
    myGroundOverlay.setDimensions(200.0f);
    myGroundOverlay.setTransparency(0.25f);
    myGroundOverlay.setBearing(0);
    mMapView.getOverlays().add(myGroundOverlay);
    

    它是“自我评论的”。

    【讨论】:

    • 只需要默认的“GroundOverlay”并且对我有用。谢谢!
    • 如果我想从 URL 加载图像,该怎么办?
    • @ShivamJamaiwar 您可以使用任何方式从 URL 加载图像(位图),例如:`URL imageUrl = new URL(image_url_string);位图图像 = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream()); myGroundOverlay.setImage(image);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多