Skip to content
Browse files

Added Google API key system.

  • Loading branch information...
1 parent dc2c117 commit 7da9a39f234ed3bc840b6d21aa9d86042fa9273d @patridge committed Jan 20, 2012
View
5 StackGeography/Services/GoogleMapsGeocodingLookupService.cs
@@ -4,6 +4,7 @@
using System.Web;
using Newtonsoft.Json;
using StackGeography.Models;
+ using System.Configuration;
public class GoogleMapsGeocodingLookupService : IGeocodingLookupService {
#pragma warning disable 649 // These fields are assigned to via JSON deserialization, so ignore "never assigned to" warnings.
@@ -46,7 +47,11 @@ public class LocationData {
}
public GeocodingLookupServiceResult Geocode(string location) {
GeocodingLookupServiceResult result;
+ string googleApiKey = ConfigurationManager.AppSettings["GoogleApiKey"];
string url = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" + HttpUtility.UrlEncode(location);
+ if (googleApiKey != null) {
+ url += "&key=" + googleApiKey;
+ }
WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
View
1 StackGeography/Web.config
@@ -5,6 +5,7 @@
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
+ <add key="GoogleApiKey" value="AIzaSyBOT4rTeK6L9WegBXUncMaQHrUULF9UtWM"/>
</appSettings>
<system.web>
View
21 StackGeography/scripts/jquery.googlemaps.js
@@ -1,4 +1,4 @@
-/*global window, jQuery, google */
+/*global window, jQuery, google, URI */
var googleMapsCallback; // Required for Google Maps API to call back when it thinks it is done (vs. when jQuery finishes loading the script file).
(function ($) {
@@ -49,13 +49,28 @@ var googleMapsCallback; // Required for Google Maps API to call back when it thi
googleMapsLoaded.resolve();
};
$.googleMaps = {};
- $.googleMaps.loadApi = function () {
+ $.googleMaps.loadApi = function (options) {
+ var opts = $.extend({}, $.googleMaps.loadApi.defaults, options),
+ url = URI(opts.url);
+ if (opts.key) {
+ url.addSearch("key", opts.key);
+ }
+ if (opts.callback) {
+ url.addSearch("callback", opts.callback);
+ }
+ url.addSearch("sensor", !!opts.sensor);
$.ajax({
- url: "/scripts/googlemapsv3.js",
+ url: url,
dataType: "script"
}).fail(googleMapsLoaded.reject);
return googleMapsLoaded.promise();
};
+ $.googleMaps.loadApi.defaults = {
+ url: "http://maps.googleapis.com/maps/api/js",
+ key: null,
+ sensor: false,
+ callback: "googleMapsCallback"
+ };
$.googleMaps.createMap = function (element, options) {
return new google.maps.Map(element, {
center: new google.maps.LatLng(options.center.lat, options.center.lng),
View
5 StackGeography/scripts/main.js
@@ -5,7 +5,10 @@ $(function () {
var stackExchangeApiKey = "BFkB32WKyHjbqI9RYU1lKA((",
stackExchangeApiFilter = "!Txfh2mrFy-p6RynMil1VXSkHECJn8fbSNFzHmEjS4am",
latestQuestionCreationDate = {},
- loadGoogleMaps = $.googleMaps.loadApi(),
+ loadGoogleMaps = $.googleMaps.loadApi({
+ key: "AIzaSyBOT4rTeK6L9WegBXUncMaQHrUULF9UtWM"
+ //, url: "/scripts/googlemapsv3.js"
+ }),
defaultMapCenterLocation = { lat: 20, lng: 0 }, // Start with a default map center.
getMapCenter = $.Deferred(function (dfd) {
$.geocode.getIpLatLng().done(function (userLocation) {

0 comments on commit 7da9a39

Please sign in to comment.
Something went wrong with that request. Please try again.