`
longgangbai
  • 浏览: 7330614 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

GIS的学习(四十六)【转】osmdroid Limiting the view to an area

阅读更多

 在地图中限制仅仅显示某一个区域,具体代码

完整代码:

  https://code.google.com/p/osmdroid/issues/attachmentText?id=209&aid=2090027000&name=BoundedMapView.java&token=CAbHEF7dJ0_5NbxdCLbdFsQ9qHI%3A1350897498709

 

讨论:

https://code.google.com/p/osmdroid/issues/detail?id=209

 

 

package com.example.yourproject.osmdroid.views;

import microsoft.mappoint.TileSystem;

import org.osmdroid.ResourceProxy;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.tileprovider.MapTileProviderBase;
import org.osmdroid.util.BoundingBoxE6;
import org.osmdroid.views.MapView;
import org.osmdroid.views.util.constants.MapViewConstants;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.widget.Scroller;

/**
 * MapView that limits scrolling to the area specified. Based on code from Marc
 * Kurtz (http://code.google.com/u/107017135012155810755/)
 * 
 */
public class BoundedMapView extends MapView {

    protected Rect mScrollableAreaLimit;
    protected BoundingBoxE6 box;

    public BoundedMapView(Context context,
                          ResourceProxy resourceProxy,
                          MapTileProviderBase provider) {

        super(context,
              provider.getTileSource().getTileSizePixels(),
              resourceProxy,
              provider);

    }

    /**
     * Set the map to limit it's scrollable view to the specified BoundingBoxE6.
     * Note that, like North/South bounds limiting, this allows an overscroll of
     * half the screen size. This means each border can be scrolled to the
     * center of the screen.
     * 
     * @param box
     *            A lat/long bounding box to limit scrolling to, or null to
     *            remove any scrolling limitations
     */
    public void setScrollableAreaLimit(BoundingBoxE6 box) {

        final int worldSize_2 = TileSystem.MapSize(MapViewConstants.MAXIMUM_ZOOMLEVEL) / 2;

        // Clear scrollable area limit if null passed.
        if (box == null) {
            mScrollableAreaLimit = null;
            return;
        }

        // Get NW/upper-left
        final Point upperLeft = TileSystem.LatLongToPixelXY(box.getLatNorthE6() / 1E6,
                                                            box.getLonWestE6() / 1E6,
                                                            MapViewConstants.MAXIMUM_ZOOMLEVEL,
                                                            null);
        upperLeft.offset(-worldSize_2, -worldSize_2);

        // Get SE/lower-right
        final Point lowerRight = TileSystem.LatLongToPixelXY(box.getLatSouthE6() / 1E6,
                                                             box.getLonEastE6() / 1E6,
                                                             MapViewConstants.MAXIMUM_ZOOMLEVEL,
                                                             null);
        lowerRight.offset(-worldSize_2, -worldSize_2);
        mScrollableAreaLimit = new Rect(upperLeft.x,
                                        upperLeft.y,
                                        lowerRight.x,
                                        lowerRight.y);
    }

    @Override
    public void scrollTo(int x, int y) {
        final int worldSize_2 = TileSystem.MapSize(this.getZoomLevel(true)) / 2;
        while (x < -worldSize_2) {
            x += worldSize_2 * 2;
        }
        while (x > worldSize_2) {
            x -= worldSize_2 * 2;
        }
        if (y < -worldSize_2) {
            y = -worldSize_2;
        }
        if (y > worldSize_2) {
            y = worldSize_2;
        }

        if (mScrollableAreaLimit != null) {
            final int zoomDiff = MapViewConstants.MAXIMUM_ZOOMLEVEL - getZoomLevel();
            final int minX = mScrollableAreaLimit.left >> zoomDiff;
            final int minY = mScrollableAreaLimit.top >> zoomDiff;
            final int maxX = mScrollableAreaLimit.right >> zoomDiff;
            final int maxY = mScrollableAreaLimit.bottom >> zoomDiff;
            if (x < minX)
                x = minX;
            else if (x > maxX)
                x = maxX;
            if (y < minY)
                y = minY;
            else if (y > maxY)
                y = maxY;
        }
        super.scrollTo(x, y);

        // do callback on listener
        if (mListener != null) {
            final ScrollEvent event = new ScrollEvent(this, x, y);
            mListener.onScroll(event);
        }
    }

    @Override
    public void computeScroll() {
        final Scroller mScroller = getScroller();
        final int mZoomLevel = getZoomLevel(false);

        if (mScroller.computeScrollOffset()) {
            if (mScroller.isFinished()) {
                /**
                 * Need to jump through some accessibility hoops here Silly
                 * enough the only thing MapController.setZoom does is call
                 * MapView.setZoomLevel(zoomlevel). But noooo .. if I try that
                 * directly setZoomLevel needs to be set to "protected".
                 * Explanation can be found at
                 * http://docs.oracle.com/javase/tutorial
                 * /java/javaOO/accesscontrol.html
                 * 
                 * This also suggests that if the subclass is made to be part of
                 * the package, this can be replaced by a simple call to
                 * setZoomLevel(mZoomLevel)
                 */
                // This will facilitate snapping-to any Snappable points.
                getController().setZoom(mZoomLevel);
            } else {
                /* correction for double tap */
                int targetZoomLevel = getZoomLevel();
                if (targetZoomLevel == mZoomLevel)
                    scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            }
            postInvalidate(); // Keep on drawing until the animation has
            // finished.
        }
    }

}

 

 

 

分享到:
评论
1 楼 apple5939 2013-02-22  
您好
  我想請問您,我用了上述的"代碼" + 範例九 的離線地圖,但我在我的程式中無法限定住"離線地圖的範圍,都會超出會有"格子"出現。

  請問您,要實現"離線地圖範圍"不能看見"格子"要怎麼做呢?
謝謝

相关推荐

    DESIGN PHILOSOPHY OF AN ISOTROPIC SIX-AXIS SERIAL.pdf

    Presented in this paper is the design philosophy employed for the construction of DIESTRO, an isotropic, six-axis, serial manipulator. The kinematic criteria applied so far in manipulator design have ...

    [Go语言入门(含源码)] The Way to Go (with source code)

    The Way to Go,: A Thorough Introduction to the Go Programming Language 英文书籍,已Cross the wall,从Google获得书中源代码,分享一下。喜欢请购买正版。 目录如下: Contents Preface......................

    麦肯锡2010.01最新报告debt and deleveraging:The global credit bubble and its economic consequences

    The recent bursting of the great global credit bubble not only led to the first worldwide recession since the 1930s but also left an enormous burden of debt that now weighs on the prospects for ...

    asp.net mvc

    AspNetMVC2_RC_VS2008.exe ASP.NET MVC 2 Release Candidate Release Notes This document describes the Release Candidate release of the ...If you are using the Areas feature, make sure to not use {area} as...

    Cyber.Forensics.eBook-EEn.pdf.7z

    specific area of interest or concern and not be forced to sequentially wade through an entire text, chapter by chapter, to get to what is important to you. In the true sense of a field manual, each ...

    Web.Client.Programming.with.Perl.Automating.Tasks.on.the.Web.pdf

    The World Wide Web has been credited with bringing the Internet to the masses. The Internet was previously the stomping ground of academics and a small, elite group of computer professionals, mostly...

    SetupEMPB85xx.exe

    The EM8500 incorporates a boost converter able to start with an input voltage as low as 300 mV and an input power of few μW. In functional mode the EM8500 operates at energy levels from a DC ...

    BOSCH氧传感器LSU4.9

    博世LSU4.9氧传感器英文详细说明书, The wide band lambda sensor LSU is a planar ZrO2 dual cell limiting current sensor with an integrated heater. It is used to measure the oxygen content and the λ-...

    Keyframe-Based Visual-Inertial Odometry Using Nonlinear Optimization.pdf

    ensuring real-time operation by limiting the optimization to a bounded window of keyframes through marginalization. Keyframes may be spaced in time by arbitrary intervals, while still related by ...

    Recover4all Professional

    If You are a company or other form of organization, and You wish to authorize the Registered Software to be used in a local area network or other network, You must purchase a license for each ...

    WizFlow网页编辑

    case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free ...

    hibernate-shards.jar

    case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free ...

    erlang 18-release

    ssl: Add possibility to downgrade an SSL/TLS connection to a tcp connection, and give back the socket control to a user process. ssh: The following new key exchange algorithms are implemented:'ecdh...

    lm1117T-3.3v

    The LM1117 is available in an adjustable version, which can set the output voltage from 1.25V to 13.8V with only two external resistors. In addition, it is also available in five fixed voltages, 1.8V,...

    Universal-USB-Installer

    these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the...

    微软内部资料-SQL性能优化3

    holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have exclusive access to the resource....

    Mobile_Image_Communication_using_JPEG2000

    In order to appropriately handle large raster imagery, the fundamental goal is to reduce the influence of the most limiting factors in mobile environments. In a clear departure from the traditional ...

    CCNA专业术语表

    access layer The layer that feeds traffic into the network and performs network entry control. End users access the network via the access layer. access A that provides remote access to a corporate ...

Global site tag (gtag.js) - Google Analytics