Mapbox add WMS source - android

Currently I am working on the adding a WMS source on the map in native android platform. I am using mapbox to show map in the application. I am trying to add a WMS source layer from Geo-server But the WMS source layer getting added multiple times over the map as shown in the picture :
Here is the code snippet which I have used to add WMS source :
#Override
public void onMapReady(MapboxMap mapboxMap) {
RasterSource webMapSource = new RasterSource(
"web-map-source",
new TileSet("tileset", "http://geo.skymetweather.com:8081/geoserver/cite/wms/cite:india_district_web?" +
"&bbox=68.036003112793,6.60812377929688,97.5504302978516," +
"37.2502937316895&format=image/png&service=WMS&version=1.1.1&" +
"request=GetMap&srs=EPSG:4326&width=493&height=512&layers=cite:india_district_web"), 256);
mapboxMap.addSource(webMapSource);
// Add the web map source to the map.
RasterLayer webMapLayer = new RasterLayer("web-map-layer", "web-map-source");
mapboxMap.addLayerBelow(webMapLayer, "aeroway-taxiway");
}
Please suggest if any thing wrong with the code or does anyone know how to add Raster Source on Map?
Thanks in advance !

Mapbox only supports 'EPSG:3857' for rendering WMS tiles and you should project your source to this SRS. Also, there is no need to set its bounding box statically as you did in second line of TileSet. Use this template to load WMS in your application:
RasterSource webMapSource = new RasterSource(
"web-map-source",
new TileSet("tileset",
'http://a.example.com/wms?bbox={bbox-epsg-3857}&format=image/png&service=WMS&version=1.1.1&request=GetMap&srs=EPSG:3857&width=256&height=256&layers=example')
,256);

Related

Android WMS: Problems generating "bbox" using the OSMDroid library

I have a problem when consulting a WMS using the OSMDroid library, the problem lies in an erroneous generation of the bbox. For example, this is a good url with a correct bbox:
https://www.ign.es/wms-inspire/pnoa-ma?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fjpeg&TRANSPARENT=true&LAYERS=OI.OrthoimageCoverage&srsname=EPSG%3A25829&TILED=true&WIDTH=256&HEIGHT=256&SRS=EPSG%3A25829&BBOX=580836.6437384554%2C4745328.435582805%2C581278.1741512696%2C4745769.96599562
However, using the OSMDroid library, It shows me a white square for the badly generated bboxes:
https://www.ign.es/wms-inspire/pnoa-ma?SERVICE=WMS&request=GetMap&width=256&height=256&version=1.1.1&layers=OI.OrthoimageCoverage&bbox=580836.6437384554%2C4745328.435582805%2C581278.1741512696%2C4745769.96599562&srs=EPSG:3857&format=image/png&transparent=true
What I tried is this code that generate a WMSTileSource, which is set on the map:
val tileSource = WMSTileSource("OI.OrthoimageCoverage", arrayOf("https://www.ign.es/wms-inspire/pnoa-ma?SERVICE=WMS&"), "OI.OrthoimageCoverage","1.1.1", "EPSG:3857", null, 256)
binding.map.setTileSource(tileSource)
This error is due to the bbox, is there any configuration that should be done in the bbox or some way to handle it in a more customized way?
This should give me a map painted like this:
However, when I compile, it just shows me a blank map:

MapBox Adding and Plotting Many Polylines Efficiently?

I have a dataset of polylines, markers and polygons stored as lat/lon data in a CSV file that need plotting in Android MapBox. I'm able to do this easily using mapboxmap.addPolyline, addMarker and addpolygons, but after adding hundreds of points scrolling the map and viewing the data on an Android phone becomes very slow.
Am I going about this wrongly? Is there a more efficient way to plot many data points that i'm supposed to be using?
What version of the Maps SDK are you using? Your use of addPolyline/addMarker/etc. makes me think that you're using a very outdated version. Please try using 8.5.0 and using sources/layers: https://docs.mapbox.com/android/maps/overview/data-driven-styling/
In general, you should use the lat/lon data in the CSV file to create Feature objects. Use them to create a FeatureCollection and then add/style layers.
Setting up a LineLayer
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
#Override
public void onStyleLoaded(#NonNull Style style) {
Feature polygonFeature = Feature.fromGeometry(Polygon.fromLngLats());
Feature lineStringFeature = Feature.fromGeometry(LineString.fromLngLats());
Feature pointFeature = Feature.fromGeometry(Point.fromLngLat());
FeatureCollection featureCollection = FeatureCollection.fromFeatures(new Feature[]{
pointPolygon, pointLineString, pointFeature
});
GeoJsonSource geoJsonSource = new GeoJsonSource("source-id", featureCollection);
style.addSource(geoJsonSource);
LineLayer lineLayer = new LineLayer("line-layer-id", "source-id");
lineLayer.setProperties(
PropertyFactory.lineColor(Color.BLUE)
);
style.addLayer(lineLayer);
}
});

Adding dynamic markers on mapbox does not work with SymbolLayer

I am currently developing android map using mapbox sdk, and I want to add multiple markers when user click on one button. and I type below in my onStyleLoaded but it's not showing anything
List<Feature> symbolLayerIconFeatureList = new ArrayList<>();
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(31.995560, 34.767070)));
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(31.995979, 34.744110)));
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(31.995010, 34.767380)));
style.addImage("charge-icon-id", BitmapFactory.decodeResource(
MapboxNavigationActivity.this.getResources(), R.drawable.map_marker_dark));
style.addSource(new GeoJsonSource("charge-source-id",
FeatureCollection.fromFeatures(symbolLayerIconFeatureList)));
SymbolLayer destinationSymbolLayer = new SymbolLayer("charge-layer-id", "charge-source-id");
destinationSymbolLayer.withProperties(iconImage("charge-icon-id"),
iconAllowOverlap(true),
iconIgnorePlacement(true)
);
style.addLayer(destinationSymbolLayer);
May I ask what are the problem?
It's a bit tough to say without some more context/information, but I'm guessing that the issue is related to the order in which you're adding the source, layer, and image. Take a look at this example: https://docs.mapbox.com/android/maps/examples/marker-symbol-layer/
You'll want to add the image, source, and layer before the style is loaded. You can then make additional adjustments/add data when the style is loaded using the onStyleLoaded listener.
Disclaimer: I work at Mapbox.

How can i use MapBox on Android [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
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.
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.
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.
Did you know that with the Google Maps API v2, you can use your own tiles provider?
See TileProvider and UrlTileProvider.
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
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.

How can I implement an offline geocoder for a single city using OSM data in android?

I am trying to develop an offline android map that makes use of OSM map tiles of a particular zoom level, For this purpose I used an open source library : Osmdroid
Now, I am looking into the possibility of creating an offline geocoding/ reverse geocoding for a single city that can be integrated with my application,
can I use Osm xml data for that purpose? if so , then can anyone suggest/explain how to use it to create SQlite db.. to be used with my app
I read here and also here about Spatialite
But cannot quite understand its working and implementation
Thanks
With the Skobbler SDK you have offline maps and (reverse) geocoding.
This has an answer in here
https://stackoverflow.com/a/64811929/13794189
and goes as follows:
To use Open Street Maps (OSM) in offline mode you need to first implement a download manager like the one shown here on the post liked bellow. It has resume capabilities which is good for very large files :
https://stackoverflow.com/a/64811752/13794189
next you can load the downloaded OSM maps file in to a mapView like this:
org.osmdroid.config.IConfigurationProvider osmConf = org.osmdroid.config.Configuration.getInstance();
File basePath = new File(SessionData.offlineMapsDirectoryPath, "osmdroid");
osmConf.setOsmdroidBasePath(basePath);
File tileCache = new File(SessionData.offlineMapsDirectoryPath, "tile");
osmConf.setOsmdroidTileCache(tileCache);
map = (MapView) getActivity().findViewById(R.id.map); // create basic map
//map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
map.setTilesScaledToDpi(true);
map.setMultiTouchControls(true);
map.setUseDataConnection(false);
// add compass to map
//CompassOverlay compassOverlay = new CompassOverlay(getActivity(), new InternalCompassOrientationProvider(getActivity()), map);
//compassOverlay.enableCompass();
//map.getOverlays().add(compassOverlay);
//attach listeners
MapEventsOverlay mapEventsOverlay = new MapEventsOverlay(this);
map.getOverlays().add(0, mapEventsOverlay);
In the code above, SessionData.offlineMapsDirectoryPath is the path where the downloaded file is saved.
Don't forget to add in your gradle file the following implementations:
implementation 'org.osmdroid:osmdroid-android:6.1.0'
implementation 'com.github.MKergall:osmbonuspack:6.6.0'

Categories

Resources