-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
157 lines (140 loc) · 6.16 KB
/
index.html
File metadata and controls
157 lines (140 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<html>
<head>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=false"></script>
<script src="https://apis.google.com/js/auth.js"></script>
<script type="text/javascript">
// An example of how to use oauth to obtain authorization for a web application
// to display Maps Engine layers readable by a user of that application.
//
// The application follows the following flow:
// 1. When first loaded, the application checks if the user has already
// authorized this application. If so, a function is called to display
// the requested layer. This check is asynchronous, so uses callbacks in
// JavaScript. If the user has authorized the application, an access_token
// is granted by oauth, which can be used to authenticate when displaying
// a Maps Engine Layer.
//
// 2. If the user has not authorized this application, an 'Authorize' button
// is shown in the UI. An oauth flow is started when the user clicks this
// button. Within this flow, the user is asked if they will grant permission
// for this application to be able to read their Maps Engine data so that
// it can be displayed on a Map.
//
// 3. If the user grants authorization to the application, the 'Authorize'
// button is hidden and the Layer is shown. If not the 'Authorize' button
// remains visible.
//
// 4. Periodically, the access_token is refreshed by making another call to the
// oauth library.
// The Client ID for your application, as configured on the Google APIs console.
var clientId = '694180608486-om9ck7s4jvdg9if21h8u8hn4643dmee3.apps.googleusercontent.com';
// The oauth scope for displaying Maps Engine data.
var scopes = 'https://www.googleapis.com/auth/mapsengine.readonly';
// The Asset ID of the Maps Engine Layer to display.
var layerId = '17239569094152164441-09507073323105492707';
// This function is run when the page (including the Maps API and Google APIs
// client libraries) have finished loading.
function initialize() {
checkAuth(false, handleAuthResult);
}
// A shared function which checks if the user has previously authorized this
// application, and if so calls the supplied callback.
// This function should always be called before calling a function which
// requires an oauth access_token.
//
// If prompt_user is true, the user will be prompted to provide access. This
// should not be set to true unless this function was triggered by a user
// action (e.g. clicking a button).
//
// If prompt_user is set to false, and the user is not authorized, the callback
// will be called with null.
function checkAuth(prompt_user, callback) {
var options = {
client_id: clientId,
scope: scopes,
// Setting immediate to 'true' will avoid prompting the user for
// authorization if they have already granted it in the past.
immediate: !prompt_user
}
// Check to see if the current user has authorized this application.
gapi.auth.authorize(options, callback);
}
// A callback run after checking if the user has authorized this application.
// If they have not, then authResult will be null, and a button will be
// displayed which the user can click to begin authorization.
//
// Authorization can only be started in response to a user action (such as
// clicking a button) in order to avoid triggering popup blockers.
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize_button');
// Has the user authorized this application?
if (authResult && !authResult.error) {
// The application is authorized. Hide the 'Authorization' button.
authorizeButton.style.display = 'none';
showMap(authResult);
} else {
// The application has not been authorized. Start the authorization flow
// when the user clicks the button.
authorizeButton.style.display = 'block';
authorizeButton.onclick = handleAuthClick;
}
}
// This function is called when the user clicks the Authorization button. It
// starts the authorization flow.
function handleAuthClick(event) {
checkAuth(true, handleAuthResult);
return false;
}
// This function is called once handleAuthResult detects that authorization
// has been provided.
function showMap(authResult) {
// Create a new Google Maps API Map
var mapOptions = {
center: new google.maps.LatLng(0,0),
zoom: 4
};
map = new google.maps.Map(
document.getElementById("map_canvas"),
mapOptions);
// Add a Maps Engine Layer to this Map. The access_token granted by the oauth
// flow is used here to access user data.
mapsEngineLayer = new google.maps.visualization.MapsEngineLayer({
layerId: layerId,
accessToken: authResult.access_token
});
mapsEngineLayer.setMap(map);
// Add an event listener which modifies the bounds of the Map to best fit
// the Layer once the layer has loaded.
google.maps.event.addListener(mapsEngineLayer, 'bounds_changed', function() {
map.fitBounds(mapsEngineLayer.get('bounds'));
});
// The access_token provided by the oauth flow is only valid for a certain
// amount of time. Add a timer which will refresh the access_token after this
// amount of time has elapsed, so that the Layer will continue to work.
window.setTimeout(refreshToken, authResult.expires_in * 1000);
}
// This function is called once the oauth token has expired. It starts an
// oauth flow in the background which obtains a new token. The user will not
// be prompted to do anything, because we set the 'immediate' property on the
// gapi.auth.authorize request to true.
function refreshToken() {
checkAuth(false, refreshLayer);
}
// This function is called once an expired access_token has been refreshed, and
// a new access_token is available.
function refreshLayer(authResult) {
// Update the token provided to the MapsEngineLayer.
mapsEngineLayer.set('accessToken', authResult.access_token);
// This token will also expire after some time, so create a timer which will
// refresh it again.
window.setTimeout(refreshToken, authResult.expires_in * 1000);
}
</script>
</head>
<body onload="initialize()">
<input type="button" value="Authorize" id="authorize_button"
style="display: none;"></input>
<div id="map_canvas" style="height: 100%; width: 100%;">
</div>
</body>
</html>