How can i use MapBox on Android [closed]

2019-03-11 22:00发布

How can i display MapBox tiles on Android ? I tried using it with OSMDroid, and Nutiteq with no success. Is there a android library for MapBox ?

For example i used this code to implement MapBox on OSMDroid : http://blog.spatialnetworks.com/post/2012/07/using-mbtiles-on-android-with-osmdroid

XYTileSource MBTILESRENDER = new XYTileSource("mbtiles", ResourceProxy.string.offline_mode, 1, 20, 256, ".png", "http://example.org/");
DefaultResourceProxyImpl mResourceProxy = new DefaultResourceProxyImpl(this.getApplicationContext());
SimpleRegisterReceiver simpleReceiver = new SimpleRegisterReceiver(this.getActivity());

File f = new File(Environment.getExternalStorageDirectory(), "mymbtilesfile.mbtiles");

IArchiveFile[] files = { MBTilesFileArchive.getDatabaseFileArchive(f) };

MapTileModuleProviderBase moduleProvider = new MapTileFileArchiveProvider(simpleReceiver, MBTILESRENDER, files);

MapTileProviderArray mProvider = new MapTileProviderArray(MBTILESRENDER, null, new     MapTileModuleProviderBase[] { moduleProvider });

this.mMapView = new MapView(this, 256, mResourceProxy, mProvider);

But it didn't work, i want to load MbTiles directly from the web.

5条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-11 22:07

Edit

Official Mapbox Android SDK is released.

Also, see Mapbox's answer.

To people who downvote, I answered the question before the sdk exists.

=============================================

I think Nutiteq looks pretty promising. Their API has a lot of features. Also, their demo projects are inline commented. You can check out this project. Especially, MBTilesMapActivity might be something that you're looking for. The class shows how to work with mbtiles:

// 1. Get the MapView from the Layout xml
mapView = (MapView) findViewById(R.id.mapView);
// 2. create and set MapView components
Components components = new Components();
mapView.setComponents(components);
// 3. Define map layer for basemap
MBTilesMapLayer dbLayer = new MBTilesMapLayer(new EPSG3857(), 0, 19, file.hashCode(), file, this);
mapView.getLayers().setBaseLayer(dbLayer);
...
// 4. Start the map - mandatory
mapView.startMapping();        
// 5. zoom buttons using Android widgets - optional
// get the zoomcontrols that was defined in main.xml
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);

You can also take a look at my toy project which is modified from their demo. Basically, my app allows the user to input a url of a mbtiles file. Then, it downloads the file and loads it to the MapView.

If you really have to stick with OSMDroid, this might be helpful. I haven't had a chance to try it though.

Hope this helps.

查看更多
做个烂人
3楼-- · 2019-03-11 22:15

Mapbox team has started pre-alfa release of Mapbox maps on Android, so I would like to recommend it, just to try. I assume it's good to know that they have heard prayers of android developers.

Mapbox for Android on Github

查看更多
小情绪 Triste *
4楼-- · 2019-03-11 22:19

OSMDroid is terribly documented, but the way they designed it makes it very flexible and self-explanitory if you look at the implementable methods. Here is how you use Mapbox tiles with OSMDroid as of January 2015.

    mvMap = (MapView) rootView.findViewById(R.id.mapview);

    String mapCode = "mapbox.streets";
    final String accessToken = "myAccessToken";

    OnlineTileSourceBase customTileSource = new XYTileSource("MapBoxSatelliteLabelled",
            ResourceProxy.string.mapquest_aerial, 1, 19, 256, ".png", new String[]{
            "http://a.tiles.mapbox.com/v4/" + mapCode + "/",
            "http://b.tiles.mapbox.com/v4/" + mapCode + "/",
            "http://c.tiles.mapbox.com/v4/" + mapCode + "/",
            "http://d.tiles.mapbox.com/v4/" + mapCode + "/"}){

        @Override
        public String getTileURLString(MapTile aTile) {
            String str = super.getTileURLString(aTile) + "?access_token=" + accessToken;
            return str;
        }
    };

    TileSourceFactory.addTileSource(customTileSource);

    mvMap.setTileSource(customTileSource);

Quick tip: If you're using Android Studio, use break points to get the value of any variable. This helps with debugging and understanding unfamiliar code. That's how I figured out that getTileURLString() returns the full URL of the tile.

查看更多
等我变得足够好
5楼-- · 2019-03-11 22:20

Did you know that with the Google Maps API v2, you can use your own tiles provider?

See TileProvider and UrlTileProvider.

查看更多
时光不老,我们不散
6楼-- · 2019-03-11 22:23

Is there a android library for MapBox ?

The best way to use Mapbox on Android is the Mapbox Android SDK! It's the official codebase, an open source project that supports vector tiles, map animation, and a bunch else - and it's also designed to be easy to transition to from the system default maps.

Re:

But it didn't work, i want to load MbTiles directly from the web.

Since an MBTiles file is a bundle of tiles that's usually quite large - and can't be requested individually - you usually won't load it directly just to show a map. The approach that the SDK takes is dynamic caching, as shown in this example. The example uses Mapbox, but the caching mechanism is general and would work with any tile server.

Disclosure: I work for Mapbox, and occasionally work on the core of our native code, including the Android SDK.

查看更多
登录 后发表回答