Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19be3fd

Browse files
committed
Pass in values from GeoJSON. Pass in credentials via config.json.
1 parent 8da3b69 commit 19be3fd

File tree

6 files changed

+244
-66
lines changed

6 files changed

+244
-66
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
config*.json
12
*.p8

‎config.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"privateKey": "AuthKey_XXXXXXXXXX.p8",
3+
"teamId": "XXXXXXXXXX",
4+
"keyId": "XXXXXXXXXX"
5+
}

‎mapkit-snapshots.js‎

Lines changed: 157 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,164 @@
77
*/
88

99
const { readFileSync } = require("fs");
10+
var fs = require('fs')
1011
const { sign } = require("jwa")("ES256"); // https://www.npmjs.com/package/jwa
1112
const 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.
22170
function 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)

‎null-island.geojson‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {
7+
"center": [20, 20],
8+
"display_point": {
9+
"type": "Point","coordinates": [10,10]
10+
},
11+
"z": 2,
12+
"spn": [1.0, 1.0],
13+
"size": [600, 400],
14+
"scale": 1,
15+
"t": "mutedStandard",
16+
"colorScheme": "dark",
17+
"poi": 1,
18+
"lang": "en-US",
19+
"annotations": [],
20+
"overlays": [],
21+
"referer": "",
22+
"expires": 3155673601
23+
},
24+
"geometry": {
25+
"type": "Point",
26+
"coordinates": [0,0]
27+
}
28+
}
29+
]
30+
}

‎package.json‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
{
2-
"name": "static-maps-cli",
3-
"version": "1.0.0",
2+
"name": "apple-maps-web-snapshot-cli",
3+
"version": "1.0.2",
44
"description": "Create static maps for MapKit",
55
"main": "mapkit-snapshots.js",
66
"bin": {
77
"mapkit-snapshots": "./mapkit-snapshots.js"
88
},
99
"dependencies": {
1010
"better-opn": "^1.0.0",
11-
"fs": "^0.0.1-security",
11+
"concat-stream": "^2.0.0",
12+
"fs": "0.0.1-security",
1213
"jwa": "^1.4.1",
13-
"opn": "^6.0.0",
14-
"open": "^7.0.0"
14+
"minimist": "^1.2.0",
15+
"open": "^7.0.0",
16+
"opn": "^6.0.0"
1517
},
1618
"devDependencies": {},
1719
"scripts": {
1820
"test": "testCommand"
1921
},
2022
"author": "roblabs",
21-
"license": "SEE LICENSE IN LICENSE.txt"
23+
"license": "SEE LICENSE IN LICENSE.txt",
24+
"repository": {
25+
"type": "git",
26+
"url": "git+https://github.com/roblabs/apple-maps-web-snapshot-cli.git"
27+
},
28+
"keywords": [],
29+
"bugs": {
30+
"url": "https://github.com/roblabs/apple-maps-web-snapshot-cli/issues"
31+
},
32+
"homepage": "https://github.com/roblabs/apple-maps-web-snapshot-cli#readme"
2233
}

‎readme.md‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,50 @@ At WWDC 2019, Apple announced Maps Web Snapshots for creating a static map from
1010

1111
Generate a URL to display a map from a Apple Maps Web Snapshot.
1212

13-
Find the source code at [roblabs/static-maps-cli](https://github.com/roblabs/static-maps-cli)
13+
Find the source code at [roblabs/apple-maps-web-snapshot-cli](https://github.com/roblabs/apple-maps-web-snapshot-cli)
14+
15+
See the sample GeoJSON, `null-island.geojson` for example valid properties
16+
17+
``` Javascript
18+
"properties": {
19+
"center": [20, 20],
20+
"display_point": {
21+
"type": "Point","coordinates": [10,10]
22+
},
23+
"z": 2,
24+
"spn": [1.0, 1.0],
25+
"size": [600, 400],
26+
"scale": 1,
27+
"t": "mutedStandard",
28+
"colorScheme": "dark",
29+
"poi": 1,
30+
"lang": "en-US",
31+
"annotations": [],
32+
"overlays": [],
33+
"referer": "",
34+
"expires": 3155673601
35+
}
36+
```
1437

1538
---
1639

1740
``` bash
1841

19-
# update your credentials in `mapkit-snapshots.js` then install and run
20-
# privateKey
21-
# teamId
22-
# keyId
42+
# update your credentials in `config.json` then install and run
43+
44+
# config.json
45+
{
46+
"privateKey": "AuthKey_XXXXXXXXXX.p8",
47+
"teamId": "XXXXXXXXXX",
48+
"keyId": "YYYYYYYYYY"
49+
}
2350

2451
# install
25-
npm install -g
52+
npm install
2653

2754
# run and inspect output
2855
# MapKit JS
29-
mapkit-snapshots
56+
mapkit-snapshots.js null-island.geojson -c config.json
3057
```
3158

3259

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /