Skip to content

Commit ba264cb

Browse files
committed
Initial bing implementation
1 parent af71295 commit ba264cb

File tree

6 files changed

+165
-115
lines changed

6 files changed

+165
-115
lines changed

dist/geocoder.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ if (function(a) {
9494

9595
case "openstreetmap":
9696
c = new a.OpenStreetMapProvider(d, b);
97+
break;
98+
99+
case "bing":
100+
c = new a.BingProvider(d, b);
97101
}
98102
return c;
99103
};
@@ -186,6 +190,54 @@ if (function(a, b) {
186190
require("../Geocoded.js"), require("../providers/ProviderBase.js");
187191
}
188192

193+
if (function(a) {
194+
"use strict";
195+
var b, c;
196+
a.BingProvider = function(a, d) {
197+
if (void 0 === a) throw "No external loader defined.";
198+
this.externalLoader = a, d = d ? d : {}, b = d.useSSL ? d.useSSL : !1, c = d.apiKey ? d.apiKey : null,
199+
c && (b = !0);
200+
}, a.BingProvider.prototype = new a.ProviderBase(), a.BingProvider.prototype.constructor = a.BingProvider,
201+
a.BingProvider.prototype.geocode = function(a, d) {
202+
this.externalLoader.setOptions({
203+
protocol: b === !0 ? "https" : "http",
204+
host: "dev.virtualearth.net",
205+
pathname: "REST/v1/Locations/" + a
206+
});
207+
var e = {
208+
key: c,
209+
JSONPCallback: "jsonp"
210+
};
211+
this.executeRequest(e, d);
212+
}, a.BingProvider.prototype.geodecode = function(a, d, e) {
213+
this.externalLoader.setOptions({
214+
protocol: b === !0 ? "https" : "http",
215+
host: "dev.virtualearth.net",
216+
pathname: "REST/v1/Locations/" + a + "," + d
217+
});
218+
var f = {
219+
key: c,
220+
JSONPCallback: "jsonp"
221+
};
222+
this.executeRequest(f, e);
223+
}, a.BingProvider.prototype.executeRequest = function(a, b) {
224+
var c = this;
225+
this.externalLoader.executeRequest(a, function(a) {
226+
var d = [];
227+
for (var e in a.resourceSets[0].resources) d.push(c.mapToGeocoded(a.resourceSets[0].resources[e]));
228+
b(d);
229+
});
230+
}, a.BingProvider.prototype.mapToGeocoded = function(b) {
231+
var c = new a.Geocoded();
232+
return c.latitude = b.point.coordinates[0], c.longitude = b.point.coordinates[1],
233+
c.streetName = b.address.addressLine, c.city = b.address.locality, c.region = b.address.adminDistrict,
234+
c.postal_code = b.address.postalCode, c;
235+
};
236+
}(GeocoderJS), "undefined" == typeof GeocoderJS && "function" == typeof require) {
237+
var GeocoderJS = require("../GeocoderJS.js");
238+
require("../Geocoded.js"), require("../providers/ProviderBase.js");
239+
}
240+
189241
if (function(a) {
190242
"use strict";
191243
var b, c;

dist/geocoder.min.js

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

example/bing.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Geocoder.js: Mapquest Simple Example</title>
5+
<script src="../dist/geocoder.js"></script>
6+
<script>
7+
var bingGeocoder = new GeocoderJS.createGeocoder({provider: 'bing', apiKey: 'As11PsBXYvAoGEXmz59ZWl93T8_OACdXi2QnRKWMRIUK6hzOXgN3BcZHnbKyPZYo'});
8+
bingGeocoder.geocode('1600 Pennsylvania Ave NW, Washington, DC', function(result) {
9+
console.log(result);
10+
});
11+
12+
setTimeout(function() {
13+
bingGeocoder.geodecode("44.915", "-93.21", function(result) {
14+
console.log(result);
15+
});
16+
}, 100);
17+
</script>
18+
</head>
19+
<body>
20+
21+
</body>
22+
</html>

src/GeocoderProviderFactory.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ if (typeof GeocoderJS === "undefined" && typeof require === "function") {
3838
case 'openstreetmap':
3939
provider = new GeocoderJS.OpenStreetMapProvider(externalLoader, options);
4040
break;
41+
case 'bing':
42+
provider = new GeocoderJS.BingProvider(externalLoader, options);
43+
break;
4144
}
4245

4346
return provider;

src/providers/BingProvider.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
if (typeof GeocoderJS === "undefined" && typeof require === "function") {
2+
var GeocoderJS = require("../GeocoderJS.js");
3+
require("../Geocoded.js");
4+
require("../providers/ProviderBase.js");
5+
}
6+
7+
;(function (GeocoderJS) {
8+
"use strict";
9+
10+
var useSSL;
11+
var apiKey;
12+
13+
GeocoderJS.BingProvider = function(_externalLoader, options) {
14+
if (_externalLoader === undefined) {
15+
throw "No external loader defined.";
16+
}
17+
this.externalLoader = _externalLoader;
18+
19+
options = (options) ? options : {};
20+
21+
useSSL = (options.useSSL) ? options.useSSL : false;
22+
apiKey = (options.apiKey) ? options.apiKey : null;
23+
24+
if (apiKey) {
25+
useSSL = true;
26+
}
27+
};
28+
29+
GeocoderJS.BingProvider.prototype = new GeocoderJS.ProviderBase();
30+
GeocoderJS.BingProvider.prototype.constructor = GeocoderJS.BingProvider;
31+
32+
GeocoderJS.BingProvider.prototype.geocode = function(searchString, callback) {
33+
this.externalLoader.setOptions({
34+
protocol: (useSSL === true) ? 'https' : 'http',
35+
host: 'dev.virtualearth.net',
36+
pathname: 'REST/v1/Locations/' + searchString
37+
});
38+
39+
var options = {
40+
key: apiKey,
41+
JSONPCallback: 'jsonp',
42+
};
43+
44+
this.executeRequest(options, callback);
45+
};
46+
47+
GeocoderJS.BingProvider.prototype.geodecode = function(latitude, longitude, callback) {
48+
this.externalLoader.setOptions({
49+
protocol: (useSSL === true) ? 'https' : 'http',
50+
host: 'dev.virtualearth.net',
51+
pathname: 'REST/v1/Locations/' + latitude + ',' + longitude
52+
});
53+
54+
var options = {
55+
key: apiKey,
56+
JSONPCallback: 'jsonp',
57+
};
58+
59+
this.executeRequest(options, callback);
60+
};
61+
62+
GeocoderJS.BingProvider.prototype.executeRequest = function(params, callback) {
63+
var _this = this;
64+
65+
this.externalLoader.executeRequest(params, function(data) {
66+
var results = [];
67+
for (var i in data.resourceSets[0].resources) {
68+
results.push(_this.mapToGeocoded(data.resourceSets[0].resources[i]));
69+
}
70+
callback(results);
71+
});
72+
};
73+
74+
GeocoderJS.BingProvider.prototype.mapToGeocoded = function(result) {
75+
var geocoded = new GeocoderJS.Geocoded()
76+
77+
geocoded.latitude = result.point.coordinates[0];
78+
geocoded.longitude = result.point.coordinates[1];
79+
80+
geocoded.streetName = result.address.addressLine;
81+
geocoded.city = result.address.locality;
82+
geocoded.region = result.address.adminDistrict;
83+
geocoded.postal_code = result.address.postalCode;
84+
85+
return geocoded;
86+
};
87+
})(GeocoderJS);

src/providers/GoogleAPIProvider.js

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -108,118 +108,4 @@ if (typeof GeocoderJS === "undefined" && typeof require === "function") {
108108

109109
return geocoded;
110110
};
111-
112-
function executeNodeRequest(params, callback) {
113-
var url = require("url"),
114-
http = useSSL ? require("https") : require("http"),
115-
urlObj = {
116-
"protocol": useSSL ? "https" : "http",
117-
"host": "maps.googleapis.com",
118-
"pathname": "maps/api/geocode/json",
119-
"query": params
120-
},
121-
requestUrl;
122-
123-
urlObj.query.sensor = "false";
124-
requestUrl = url.format(urlObj);
125-
126-
http.get(requestUrl, function(res) {
127-
if (res.statusCode != 200) {
128-
throw("Received HTTP status code " + res.statusCode + " when attempting geocoding request.");
129-
}
130-
131-
res.data = "";
132-
res.setEncoding("utf8");
133-
134-
res.on("data", function (chunk) {
135-
res.data += chunk;
136-
});
137-
138-
res.on("end", function () {
139-
if (!res.data || !res.data.length) {
140-
throw("Received empty data when attempting geocoding request.");
141-
}
142-
143-
var data = false,
144-
i = 0,
145-
results = [];
146-
try {
147-
data = JSON.parse(res.data);
148-
}
149-
catch(e) {
150-
throw("Received invalid JSON data when attempting geocoding request.");
151-
}
152-
153-
if (data && data.status) {
154-
if (data.status === "OVER_QUERY_LIMIT") {
155-
throw("Exceeded daily quota when attempting geocoding request.");
156-
}
157-
else if (data.status === "OK" && data.results) {
158-
for (; i < data.results.length; i++) {
159-
results.push(GeocoderJS.GoogleAPIProvider.prototype.mapToGeocoded(data.results[i]));
160-
}
161-
return callback(results);
162-
}
163-
}
164-
165-
throw("Received unexpected JSON data when attempting geocoding request.");
166-
});
167-
}).on("error", function(err) {
168-
throw(err);
169-
});
170-
}
171-
172-
function executeDOMRequest(params, callback) {
173-
var req = new XMLHttpRequest(),
174-
requestUrl = (useSSL ? "https" : "http") + "://maps.googleapis.com/maps/api/geocode/json?sensor=false&";
175-
176-
for (var key in params) {
177-
if (params.hasOwnProperty(key)) {
178-
requestUrl += encodeURIComponent(key) + "=" + encodeURIComponent(params[key]) + '&';
179-
}
180-
}
181-
182-
req.onload = function () {
183-
if (this.status != 200) {
184-
console.log("Received HTTP status code " + this.status + " when attempting geocoding request.");
185-
return callback(null);
186-
}
187-
188-
if (!this.responseText || !this.responseText.length) {
189-
console.log("Received empty data when attempting geocoding request.");
190-
return callback(null);
191-
}
192-
193-
var data = false,
194-
i = 0,
195-
results = [];
196-
try {
197-
data = JSON.parse(this.responseText);
198-
}
199-
catch(e) {
200-
console.log("Received invalid JSON data when attempting geocoding request.");
201-
return callback(null);
202-
}
203-
204-
if (data && data.status) {
205-
if (data.status === "OVER_QUERY_LIMIT") {
206-
console.log("Exceeded daily quota when attempting geocoding request.");
207-
return callback(null);
208-
}
209-
else if (data.status === "OK" && data.results) {
210-
for (; i < data.results.length; i++) {
211-
results.push(GeocoderJS.GoogleAPIProvider.prototype.mapToGeocoded(data.results[i]));
212-
}
213-
return callback(results);
214-
}
215-
}
216-
217-
console.log("Received unexpected JSON data when attempting geocoding request.");
218-
return callback(null);
219-
};
220-
221-
req.open("GET", requestUrl);
222-
req.send();
223-
}
224-
225111
})(GeocoderJS);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy