77*/
88
99const { readFileSync } = require ( "fs" ) ;
10+ var fs = require ( 'fs' )
1011const { sign } = require ( "jwa" ) ( "ES256" ) ; // https://www.npmjs.com/package/jwa
1112const opn = require ( 'better-opn' ) ; // https://www.npmjs.com/package/better-opn
13+ var argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) // parse argument options
14+ var concat = require ( 'concat-stream' )
15+ var stdin
16+ var usage = "Usage:\n mapkit-snapshots.js <file.geojson> -c config.json # pass in privateKey, teamId, keyId"
17+ var signThis ;
18+ var privateKey , teamId , keyId ;
1219
13- /* Read your private key from the file system. (Never add your private key
14- * in code or in source control. Always keep it secure.)
15- */
16- const privateKey = readFileSync ( "AuthKey_XXXXXXXXXX.p8" ) ;
17- // Replace the team ID and key ID values with your actual values.
18- const teamId = "XXXXXXXXXX" ;
19- const keyId = "XXXXXXXXXX" ;
20+ 21+ if ( argv . help || argv . h ) {
22+ console . log ( usage )
23+ process . exit ( )
24+ }
25+ 26+ // process `config.json`
27+ if ( argv . c ) {
28+ var config ;
29+ 30+ configJson = fs . createReadStream ( argv . c )
31+ configJson . pipe (
32+ concat ( function ( buffer ) {
33+ try {
34+ config = JSON . parse ( buffer )
35+ console . log ( config ) ;
36+ /* Read your private key from the file system. (Never add your private key
37+ * in code or in source control. Always keep it secure.)
38+ */
39+ privateKey = readFileSync ( config . privateKey ) ;
40+ // Replace the team ID and key ID values with your actual values.
41+ teamId = config . teamId ;
42+ keyId = config . keyId ;
43+ } catch ( e ) {
44+ return console . error ( e )
45+ }
46+ } )
47+ ) ;
48+ 49+ }
50+ 51+ if ( argv . _ [ 0 ] && argv . _ [ 0 ] !== '-' ) {
52+ stdin = fs . createReadStream ( argv . _ [ 0 ] )
53+ } else if ( ! process . stdin . isTTY || argv . _ [ 0 ] === '-' ) {
54+ stdin = process . stdin
55+ } else {
56+ console . log ( usage )
57+ process . exit ( 1 )
58+ }
59+ 60+ // buffer all input
61+ stdin . pipe (
62+ concat ( function ( buffer ) {
63+ try {
64+ var geojson = JSON . parse ( buffer )
65+ } catch ( e ) {
66+ return console . error ( e )
67+ }
68+ 69+ // Example 1: output each feature element
70+ geojson . features . forEach ( element => {
71+ 72+ 73+ // https://developer.apple.com/documentation/snapshots/create_a_maps_web_snapshot#query-parameters
74+ if ( element . geometry . type === "Point" ) {
75+ 76+ // Determine the center point of the static map in this order
77+ // 1. The IMDF property, properties.display_point
78+ // 2. properties.center
79+ // 3. If Point, then use Point
80+ var lat , long ;
81+ if ( 'display_point' in element . properties ) {
82+ lat = element . properties . display_point . coordinates [ 1 ] ;
83+ long = element . properties . display_point . coordinates [ 0 ] ;
84+ } else if ( 'center' in element . properties ) {
85+ lat = element . properties . center [ 1 ] ;
86+ long = element . properties . center [ 0 ] ;
87+ } else {
88+ lat = element . geometry . coordinates [ 1 ] ;
89+ long = element . geometry . coordinates [ 0 ] ;
90+ }
91+ signThis = `center=${ lat } ,${ long } &` ;
92+ console . log ( signThis )
93+ 94+ // Handle cases where both `z` &`spn` are present.
95+ // Apple > When both z and spn are provided, spn takes precedence over z.
96+ if ( 'spn' in element . properties ) {
97+ latDegrees = element . properties . spn [ 0 ] ;
98+ lonDegrees = element . properties . spn [ 1 ] ;
99+ signThis += `spn=${ latDegrees } ,${ lonDegrees } &`
100+ console . log ( signThis )
101+ } else if ( 'z' in element . properties ) {
102+ signThis += `z=${ element . properties . z } &`
103+ console . log ( signThis )
104+ }
105+ 106+ if ( 'size' in element . properties ) {
107+ width = element . properties . size [ 0 ] ;
108+ height = element . properties . size [ 1 ] ;
109+ signThis += `size=${ width } x${ height } &`
110+ console . log ( signThis )
111+ }
112+ 113+ if ( 'scale' in element . properties ) {
114+ signThis += `scale=${ element . properties . scale } &`
115+ console . log ( signThis )
116+ }
117+ 118+ if ( 't' in element . properties & element . properties . t != "" ) {
119+ signThis += `t=${ element . properties . t } &`
120+ console . log ( signThis )
121+ }
122+ 123+ if ( 'colorScheme' in element . properties & element . properties . colorScheme != "" ) {
124+ signThis += `colorScheme=${ element . properties . colorScheme } &`
125+ console . log ( signThis )
126+ }
127+ 128+ if ( 'poi' in element . properties ) {
129+ signThis += `poi=${ element . properties . poi } &`
130+ console . log ( signThis )
131+ }
132+ 133+ if ( 'lang' in element . properties & element . properties . lang != "" ) {
134+ signThis += `lang=${ element . properties . lang } &`
135+ console . log ( signThis )
136+ }
137+ 138+ // if ('annotations' in element.properties) {
139+ // signThis += `annotations=${element.properties.colorScheme}&`
140+ // Each query parameter must be URL-encoded.
141+ // encodeAnnotations = encodeURIComponent(JSON.stringify(annotations));
142+ // console.log(signThis)
143+ // }
144+ 145+ // if ('overlays' in element.properties) {
146+ // signThis += `overlays=${element.properties.colorScheme}&`
147+ // Each query parameter must be URL-encoded.
148+ // encodeOverlays = encodeURIComponent(JSON.stringify(overlays));
149+ // console.log(signThis)
150+ // }
151+ 152+ if ( 'referer' in element . properties & element . properties . referer != "" ) {
153+ signThis += `referer=${ element . properties . referer } &`
154+ console . log ( signThis )
155+ }
156+ 157+ if ( 'expires' in element . properties ) {
158+ signThis += `expires=${ element . properties . expires } &`
159+ console . log ( signThis )
160+ }
161+ 162+ }
163+ } ) ;
164+ 165+ signIt ( signThis )
166+ } )
167+ )
20168
21169// Creates the signature string and returns the full Snapshot request URL including the signature.
22170function signIt ( params ) {
@@ -31,53 +179,9 @@ function signIt(params) {
31179
32180 // Optionally open the result in the default browser using `opn`
33181 // opn(url);
182+ console . log ( )
34183 console . log ( url ) ;
184+ console . log ( )
35185
36186 return url ;
37187}
38- 39- 40- // Call the signIt function with a simple map request.
41- 42- /*
43- ``` bash
44- node mapkit-snapshots.js > tmp/out.md
45- ```
46- */
47- 48- var signedMapsWebSnapshotURL ;
49- 50- // See `readme.md` for live examples and example parameters
51- 52- // Yosemite
53- signedMapsWebSnapshotURL = signIt ( "center=37.839622,-119.515182" )
54- 55- // Annotations to be displayed on the map, specified as an array of JSON Annotation objects.
56- annotations = [
57- { "point" :"32.732373,-117.197503" , "color" :"blue" , "glyphText" :"A" , "markerStyle" :"large" } ,
58- { "point" :"32.715104,-117.174038" , "color" :"00ff00" , "glyphText" :"9" , "markerStyle" :"balloon" } ,
59- { "point" :"32.699945,-117.169792" , "color" :"red" , "glyphText" :"a" , "markerStyle" :"dot" }
60- ] ;
61- 62- // An array of overlays to be displayed on the map, specified as an array of JSON Overlay objects.
63- overlays = [
64- {
65- "points" : [ "32.732373,-117.197503" , "32.715104,-117.174038" , "32.699945,-117.169792" ] ,
66- "strokeColor" : "ff0000" , "lineWidth" : 2 , "lineDash" : [ 10 , 5 ]
67- }
68- ] ;
69- 70- // Each query parameter must be URL-encoded.
71- encodeAnnotations = encodeURIComponent ( JSON . stringify ( annotations ) ) ;
72- encodeOverlays = encodeURIComponent ( JSON . stringify ( overlays ) ) ;
73- 74- // Annotations example
75- signedMapsWebSnapshotURL = signIt ( "center=San%20Diego,%20California&annotations=" + encodeAnnotations )
76- 77- // Overlays example
78- signedMapsWebSnapshotURL = signIt ( "center=San%20Diego,%20California&overlays=" + encodeOverlays )
79- 80- // Annotations & Overlays
81- signedMapsWebSnapshotURL = signIt ( "center=San%20Diego,%20California" +
82- "&annotations=" + encodeAnnotations +
83- "&overlays=" + encodeOverlays )
0 commit comments