encho253

Untitled

Apr 18th, 2017
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let locationTag = $("#enter");
  2. let mapHolder = $("#mapholder");
  3.  
  4. function getLocation() {
  5.     return new Promise((resolve, reject) => {
  6.         navigator.geolocation.getCurrentPosition(function(position) {
  7.             resolve(position);
  8.         }, function(error) {
  9.             reject(error);
  10.         });
  11.     });
  12. }
  13.  
  14. function showPosition(position) {
  15.     var latlon = position.coords.latitude + "," + position.coords.longitude;
  16.  
  17.     var img_url = "https://maps.googleapis.com/maps/api/staticmap?center=" +
  18.         latlon + "&zoom=14&size=400x300&sensor=false&key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU";
  19.     mapHolder.html("<img src='" + img_url + "'>");
  20. }
  21.  
  22. function showError(error) {
  23.     switch (error.code) {
  24.         case error.PERMISSION_DENIED:
  25.             mapHolder.html("User denied the request for Geolocation.");
  26.             break;
  27.         case error.POSITION_UNAVAILABLE:
  28.             mapHolder.html("Location information is unavailable.");
  29.             break;
  30.         case error.TIMEOUT:
  31.             mapHolder.html("The request to get user location timed out.")
  32.             break;
  33.         case error.UNKNOWN_ERROR:
  34.             mapHolder.html("An unknown error occurred.");
  35.             break;
  36.     }
  37. }
  38.  
  39. locationTag.on("click", () => {
  40.     getLocation()
  41.         .then(showPosition,
  42.             showError);
  43. });
Advertisement
Add Comment
Please, Sign In to add comment