With Pas2JS, you can use the Pascal TMap class, which provides access to most of Mapbox's Map API.
Unzip this file anywhere you like; no installation is needed.
Unzip this file anywhere you like; no installation is needed.
Here's a very simple JavaScript app that uses the Mapbox GL library:
mapboxgl.accessToken = 'your-access-token-goes-here';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/satellite-streets-v10',
center: [-107.74, 37.92],
zoom: 9
});
See the Map API for information about the map options used.
To test this code, do the following:
program SimpleMap;
uses
JS,
MapboxGL;
var
map : TMap;
begin
accessToken := 'your-access-token-goes-here';
map := TMap.New(new(['container', 'map',
'style', 'mapbox://styles/mapbox/satellite-streets-v10',
'center', TJSArray._of(-107.74, 37.92),
'zoom', 9.0]));
end.
See the mapboxgl.pas interface unit in the packages/mapbox subdirectory of the PnJ download for how accessToken and TMap are declared external. The JavaScript implementations are actually in file mapbox-gl.js that you reference in your app's HTML file.
Note the nifty new function (in the JS unit) for creating JavaScript objects (equivalent to { } ). And the handy TJSArray._of class function for creating JavaScript arrays (equivalent to [ ] ).
To test this code, do the following:
yourpath/bin/pas2js -Jirtl.js -Jc ../../../packages/mapbox simple-map.pas
BoxCast P2J demonstrates how to do the following things in Pascal:
Edit boxcast_config.pas to use a non-local Web server.
Note that you can run Boxcast P2J without doing this step, but you won't be able to retrieve any forecast data. (See the note below for more.)
yourpath/bin/pas2js -Jirtl.js -Jc -O- -Fu../../../packages/mapbox boxcast_p2j.pas
You can reduce the amount of generated JavaScript by using the default optimizations. However, this can have unintended consequences. For example, with BoxCast P2J, pas2js strips out methods that it thinks are unused, but are called by the Mapbox library. As a result, optimizations have to be disabled with the -O- switch for this example.
import httplib, urllib, sys
params = urllib.urlencode([
('code_url', sys.argv[1]),
('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
('output_format', 'text'),
('output_info', 'compiled_code'),
])
headers = { "Content-type": "application/x-www-form-urlencoded" }
conn = httplib.HTTPSConnection('closure-compiler.appspot.com')
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
data = response.read()
print data
conn.close()
python obfuscate.py https://www.mydomain.com/boxcast_p2j.debug.js >boxcast_p2j.js
The resulting smaller boxcast_p2j.js file is what you would deploy instead of the original boxcast_p2j.debug.js.
Here's what BoxCast P2J does now, where fcstUrl is the CGI URL from boxcast_config.pas:
{Make AJAX call to server, passing coordinates of clicked point.
corslite will call the callback function with server response.}
corslite(appSettings.fcstUrl + '?lat1=' + string(JSValue(e.lngLat.lat)) +
'&lon1=' + string(JSValue(e.lngLat.lng)) + '&geojson=true',
@CorsCallback, True);
Just change it to something like this:
corslite('https://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?' +
'lat=' + string(JSValue(e.lngLat.lat)) +
'&lon=' + string(JSValue(e.lngLat.lng)) + '&format=24+hourly',
@CorsCallback, True);
Now all you would need to do is change CorsCallback to parse the returned XML data and convert it to the GeoJSON data that BoxCast expects.
The problem is that the weather server does not support CORS and so most browsers block this kind of cross-domain request. By making the call from a CGI app, the browser is not involved and thus can't block the request. (Note that the server where you deploy the CGI app will still need to support CORS.)
macpgmr (at) icloud (dot) com
First posted March 16, 2018; last edited April 26, 2018.