-
Notifications
You must be signed in to change notification settings - Fork 49
/
reference-to-place_id-migration.html
88 lines (79 loc) · 2.73 KB
/
reference-to-place_id-migration.html
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
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=[YOUR-API-KEY]"></script>
</head>
<body>
<div id="map"></div>
<p>
Google has <a href="https://developers.google.com/places/web-service/field-migration">depreciated the <strong>reference</strong> field</a>.
</p>
<p>
This is just a little helper script to help you convert your <strong>reference</strong> fields to the new <strong>place_id</strong> field.
</p>
<p>
You'll need to make a few changes to this script for it to work for you.
</p>
<ol>
<li>Change the <strong>[YOUR-API-KEY]</strong>with your own Google API key</li>
<li>Add the references you need to lookup in the array, see <strong>var referencesToMigrate</strong></li>
<li>If you're updating a database, look for <strong>var sqlTemplate</strong> and change the table and field references</li>
</ol>
<textarea id="converted" rows="50" cols="80"></textarea>
</body>
<script>
var referencesToMigrate = [
"<your Google Places reference>"
];
var converted = [];
function ready(fn) {
if (document.readyState != 'loading'){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
function showConverted() {
var sqlTemplate = "update <YOUR-TABLE> set <YOUR-REFERENCE-COLUMN> = '{NEW_REF}' where <YOUR-REFERENCE-COLUMN>> = '{OLD_REF}';\n" + String.fromCharCode(13);
var sql = "";
console.table(converted);
for (var i=0; i < converted.length; i++) {
var item = converted[i];
if (item.changed) {
sql += sqlTemplate.replace('{OLD_REF}', item.reference).replace('{NEW_REF}', item.place_id);
}
}
document.getElementById("converted").innerText = sql;
}
function doStuff() {
var map = new google.maps.Map(document.getElementById("map"));
var service = new google.maps.places.PlacesService(map);
for (var i=0; i < referencesToMigrate.length; i++) {
// Keep within request limits ...
setTimeout(function() {
var refToMigrate = referencesToMigrate.pop();
var request = {
reference: refToMigrate
};
console.log("service.getDetails(request." + refToMigrate + ")");
service.getDetails(request, function(placeDetails, status) {
if (status === "OVER_QUERY_LIMIT") {
console.error("Query over limits");
return;
}
converted.push({
reference: refToMigrate,
place_id: placeDetails.place_id,
changed: (refToMigrate !== placeDetails.place_id)
});
if (referencesToMigrate.length === 0) {
// list exhausted so show results
showConverted();
}
});
}, (i+1) * 1000);
} // for
} // doStuff
ready(doStuff);
</script>
</html>