Skip to content

Commit

Permalink
Merge pull request #261 from lbaey/json-style
Browse files Browse the repository at this point in the history
Allow Mapbox to load local json map style definition
  • Loading branch information
EddyVerbruggen authored Sep 25, 2018
2 parents 4ac99bb + 0c2623b commit 500ff13
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ All currently supported options for your XML based map are (__don't__ use other
|---|---|---
|`accesstoken`|-|see 'Prerequisites' above
|`delay`|0|A delay in milliseconds - you can set this to have better control over when Mapbox is invoked so it won't clash with other computations your app may need to perform.
|`mapStyle`|streets|streets, light, dark, satellite_streets, satellite, traffic_day, traffic_night
|`mapStyle`|streets|streets, light, dark, satellite_streets, satellite, traffic_day, traffic_night, an URL starting with mapbox:// or pointing to a custom JSON definition (http://, https://, or local relative to nativescript app path ~/)
|`latitude `|-|Set the center of the map by passing this in
|`longitude`|-|.. and this as well
|`zoomLevel`|0|0-20
Expand Down
30 changes: 30 additions & 0 deletions demo/app/OSM-map-style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": 8,
"name": "OpenStreetMap Tiles",
"sources": {
"OSMTileLayer": {
"type": "raster",
"attribution": "(c) OpenStreetMap contributors, CC-BY-SA",
"scheme": "xyz",
"tiles": [
"https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
"https://b.tile.openstreetmap.org/{z}/{x}/{y}.png",
"https://c.tile.openstreetmap.org/{z}/{x}/{y}.png"
],
"minzoom": 0,
"maxzoom": 18,
"tileSize": 256,
"bounds": [ -180, -85, 180, 85 ]
}
},
"layers": [
{
"id": "OSMTileLayer",
"type": "raster",
"source": "OSMTileLayer",
"paint": {
"raster-fade-duration": 100
}
}
]
}
3 changes: 3 additions & 0 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function onMapReady(args) {
// this works perfectly fine, but generates a lot of noise
// map.setOnScrollListener((point?: LatLng) => console.log(`Map scrolled: ${JSON.stringify(point)}`));

// this allows json style loading for XYZ or TMS tiles source
// map.setMapStyle("~/OSM-map-style.json");

// .. or use the convenience methods exposed on args.map, for instance:
map.addMarkers([
{
Expand Down
9 changes: 6 additions & 3 deletions src/mapbox.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ export class MapboxView extends MapboxViewBase {
const _getMapStyle = (input: any): any => {
const Style = com.mapbox.mapboxsdk.constants.Style;
// allow for a style URL to be passed
if (/^mapbox:\/\/styles/.test(input)) {
if (/^mapbox:\/\/styles/.test(input) || /^http:\/\//.test(input) || /^https:\/\//.test(input)) {
return input;
}
if (input === MapStyle.LIGHT || input === MapStyle.LIGHT.toString()) {
} else if (/^~\//.test(input)) {
let assetsPath = 'asset://app/';
input = input.replace(/^~\//, assetsPath);
return input;
} else if (input === MapStyle.LIGHT || input === MapStyle.LIGHT.toString()) {
return Style.LIGHT;
} else if (input === MapStyle.DARK || input === MapStyle.DARK.toString()) {
return Style.DARK;
Expand Down
7 changes: 5 additions & 2 deletions src/mapbox.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ const _setMapboxMapOptions = (mapView: MGLMapView, settings) => {
};

const _getMapStyle = (input: any): NSURL => {
if (/^mapbox:\/\/styles/.test(input)) {
// allow for a style URL to be passed
if (/^mapbox:\/\/styles/.test(input) || /^http:\/\//.test(input) || /^https:\/\//.test(input)) {
return NSURL.URLWithString(input);
} else if (/^~\//.test(input)) {
var assetPath = 'file://' + fs.knownFolders.currentApp().path + '/';
input = input.replace(/^~\//, assetPath);
return NSURL.URLWithString(input);
} else if (input === MapStyle.LIGHT || input === MapStyle.LIGHT.toString()) {
return MGLStyle.lightStyleURL;
Expand Down

0 comments on commit 500ff13

Please sign in to comment.