Skip to content

Commit 9a5d8b4

Browse files
committed
feat(map): Added support for map embed with option place, view and streetview
1 parent 5abdb64 commit 9a5d8b4

File tree

7 files changed

+416
-253
lines changed

7 files changed

+416
-253
lines changed

build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"SMILEY" : true,
77
"HIGHLIGHTCODE" : true,
88

9+
"MAP" : true,
910

1011
"YOUTUBE" : true,
1112
"VIMEO" : true,

demo/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ <h1>Loading embed.js in a block</h1>
3636

3737
<div id="block">
3838
Pulchritudines nocere, :heart: tanquam talis hibrida.A falsis, brodium :+1: salvus gabalium.Nunquam
39-
promissio
39+
promissio @(iit roorkee)
4040
http://iitr.ac.in caesium.Cum rumor
4141
https://twitter.com/IGN/status/652941146054881281
4242
peregrinationes https://www.flickr.com/photos/xdjio/226228060/, omnes ususes http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_50mb.mp4
@@ -69,7 +69,8 @@ <h1>Loading embed.js in a block</h1>
6969
element: document.getElementById('block'),
7070
videoDetails:false,
7171
videoJS:true,
72-
videoWidth:720
72+
videoWidth:720,
73+
googleAuthKey:'AIzaSyCqFouT8h5DKAbxlrTZmjXEmNBjC69f0ts',
7374
});
7475
x.render();
7576
x.text(function(data){

dist/embed.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js

Lines changed: 346 additions & 246 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/embed.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/embed.es6

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if (build.SMILEY) var Smiley = require('./modules/emoticons/smiley.es6');
2626
if (build.LINK) var Url = require('./modules/url.es6');
2727

2828
if (build.TWITTER) var Twitter = require('./modules/twitter/twitter.es6');
29+
if (build.MAP) var Gmap = require('./modules/map/map.es6');
2930

3031
const Code = require('./modules/code/code.es6');
3132
const Video = require('./modules/video/video.es6');
@@ -54,6 +55,10 @@ const helper = require('./modules/video/helper.es6');
5455
fluid : true,
5556
preload : 'metadata'
5657
},
58+
locationEmbed : true,
59+
mapOptions : {
60+
mode : 'place'
61+
},
5762
tweetsEmbed: true,
5863
tweetOptions: {
5964
maxWidth : 550,
@@ -124,7 +129,7 @@ const helper = require('./modules/video/helper.es6');
124129
output = options.fontIcons && build.SMILEY ? (new Smiley(output, options).process()) : output;
125130
[output, embeds] = (new Code(input, output, options, embeds).process());
126131
[output, embeds] = await (new Video(input, output, options, embeds).process());
127-
132+
[output,embeds] = options.locationEmbed ? await (new Gmap(input, output, options, embeds).process()) : [output, embeds];
128133
[output, embeds] = (new Audio(input, output, options, embeds).process());
129134
[output, embeds] = (new Image(input, output, options, embeds).process());
130135
if (options.tweetsEmbed && build.TWITTER) {

src/js/modules/map/map.es6

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const utils = require('../utils.es6');
2+
3+
class Gmap {
4+
constructor(input, output, options, embeds) {
5+
this.input = input;
6+
this.output = output;
7+
this.options = options;
8+
this.embeds = embeds;
9+
this.regex = /@\((.+)\)/gi;
10+
}
11+
12+
async getCoordinate(location) {
13+
let url = `http://maps.googleapis.com/maps/api/geocode/json?address=${location}&sensor=false`;
14+
let response = await fetch(url);
15+
let data = await response.json();
16+
console.log(data);
17+
let [latitude, longitude] = [data.results[0].geometry.location.lat, data.results[0].geometry.location.lng];
18+
return [latitude, longitude]
19+
}
20+
21+
template(match, latitude, longitude) {
22+
let template;
23+
let location = match.split('(')[1].split(')')[0];
24+
let config = this.options.mapOptions;
25+
let dimensions = utils.dimensions(this.options);
26+
if (config.mode === 'place') {
27+
template =
28+
`<div class="ejs-embed ejs-map"><iframe width="${dimensions.width}" height="${dimensions.height}" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=${this.options.googleAuthKey}&q=${location}"></iframe></div>`;
29+
} else if (config.mode === 'streetview') {
30+
template =
31+
`<div class="ejs-embed ejs-map"><iframe width="${dimensions.width}" height="${dimensions.height}" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/streetview?key=${this.options.googleAuthKey}&location=${latitude},${longitude}&heading=210&pitch=10&fov=35"></iframe></div>`;
32+
} else if (config.mode === 'view') {
33+
template =
34+
`<div class="ejs-embed ejs-map"><iframe width="${dimensions.width}" height="${dimensions.height}" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/view?key=${this.options.googleAuthKey}&center=${latitude},${longitude}&zoom=18&maptype=satellite"></iframe></div>`
35+
}
36+
return template;
37+
}
38+
39+
async process() {
40+
let match;
41+
while ((match = utils.matches(this.regex, this.input)) !== null) {
42+
let [latitude, longitude] = this.options.mapOptions.mode !== 'place' ? await this.getCoordinate(match[0]) : [null, null];
43+
let text = this.template(match[0], latitude, longitude);
44+
this.embeds.push({
45+
text: text,
46+
index: match.index
47+
})
48+
}
49+
this.output = this.output.replace(this.regex, function(match) {
50+
return '<span class="ejs-location">' + match.split('(')[1].split(')')[0] + '</span>';
51+
});
52+
return [this.output, this.embeds];
53+
}
54+
}
55+
56+
module.exports = Gmap;

0 commit comments

Comments
 (0)