Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- requirejs.config({
- baseUrl: ".",
- paths: {
- app: "./script.js"
- }
- });
- function init_map() {
- var map = new google.maps.Map(document.getElementById("map"), {
- center: { lat: -34.397, lng: 150.644 },
- zoom: 6
- });
- var info_window = new google.maps.InfoWindow;
- // Try HTML5 Geolocation
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- var pos = {
- lat: position.coords.latitude,
- lng: position.coords.longitude
- };
- var form = create_form();
- var input1 = create_input("text", "current_amount", "Amount");
- var select1 = create_select("from_currency", "from", "From");
- var select2 = create_select("to_currency", "to", "To");
- var xreq = xml_request(
- "https://openexchangerates.org/api/currencies.json?prettyprint=true&show_alternative=false&show_inactive=false&app_id=MY_APP_ID",
- function(response) {
- var results = response, arr, i, len, key, val;
- for (i = 0, arr = Object.keys(results), len = arr.length; i < len; ++i) {
- key = arr[i];
- val = results[key];
- var option = document.createElement("option");
- option.name = key;
- option.id = key;
- option.innerHTML = key + " - " + val;
- select1.appendChild(option);
- select2.appendChild(option);
- }
- for (var i = 0; i < select1.options.length; i++) {
- if (select1.options[i] == results["USD"]) {
- select1.options[i].selected = "selected";
- select1.options[i].disabled = "disabled";
- }
- }
- var geo_coder = new google.maps.Geocoder();
- var currency_abbr = require("./scripts/country-currency-map.min.js").getCurrencyAbbreviation;
- geo_coder.geocode({ "location": pos }, function (geocode_results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- if (geocode_results[1]) {
- var country = null;
- for (var r = 0, rl = geocode_results.length; r < rl; r++) {
- var result = geocode_results[r]
- if (!country && result.types[0] === "country") {
- country = result.address_components[0].long_name;
- currency_abbr(country);
- }
- }
- }
- }
- });
- for (var i = 0; i < select2.options.length; i++) {
- if (select2.options[i] == results[currency_abbr]) {
- select2.options[i].selected = "selected";
- }
- }
- });
- var input2 = create_input("submit", "submit", "Convert");
- form.appendChild(input1);
- form.appendChild(select1);
- form.appendChild(select2);
- form.appendChild(input2);
- info_window.setPosition(pos);
- info_window.setContent(form);
- info_window.open(map);
- map.setCenter(pos);
- }, function() {
- handle_location_error(true, info_window, map.getCenter());
- });
- } else {
- // Browser doesn't support Geolocation
- handle_location_error(false, info_window, map.getCenter());
- }
- }
- function handle_location_error(browser_has_geolocation, info_window, pos) {
- info_window.setPosition(pos);
- info_window.setContent(browser_has_geolocation ? "Error: The Geolocation service failed." : "Error: Your browser doesn't support Geolocation");
- info_window.open(map);
- }
- function create_form() {
- var form = document.createElement("form");
- form.method = "post";
- form.action = "/currency_converter.exe";
- return form;
- }
- function create_input(type, name, value) {
- var input = document.createElement("input");
- input.type = type;
- input.name = name;
- input.value = value;
- return input;
- }
- function create_select(name, id, value) {
- var select = document.createElement("select");
- select.name = name;
- select.id = id;
- select.value = value;
- return select;
- }
- function xml_request(url, success_callback) {
- var xreq = new XMLHttpRequest();
- xreq.withCredentials = true;
- // Check if the XMLHttpRequest object has a "withCredentials" property.
- // "withCredentials" only exists on XMLHTTPRequest2 objects.
- if ("withCredentials" in xreq) {
- xreq.open("GET", url, true);
- // Otherwise, check if XDomainRequest.
- // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
- } else if (typeof XDomainRequest != "undefined") {
- xreq = new XDomainRequest();
- xreq.open("GET", url, true);
- // Otherwise, CORS is not supported by the browser.
- } else {
- xreq = null;
- }
- if (typeof xreq != null) {
- xreq.setRequestHeader("Accept", "application/json");
- xreq.setRequestHeader("Accept-Charset", "utf-8");
- xreq.setRequestHeader("Accept-Language", "en-US");
- xreq.responseType = "json";
- xreq.onload = function() {
- if (xreq.status == 200) {
- success_callback(this.response);
- } else {
- form.innHTML = "Something went wrong";
- }
- };
- xreq.onerror = function() {
- console.log("There was an error!");
- };
- xreq.send({ form: "data" });
- }
- return xreq;
- }
Add Comment
Please, Sign In to add comment