encho253

Untitled

Apr 18th, 2017
521
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. const promise = new Promise((resolve, reject) => {
  5.     navigator.geolocation.getCurrentPosition(function(position) {
  6.         resolve(position);
  7.     }, function(error) {
  8.         reject(error);
  9.     });
  10. });
  11.  
  12. function showPosition(position) {
  13.     var latlon = position.coords.latitude + "," + position.coords.longitude;
  14.  
  15.     var img_url = "https://maps.googleapis.com/maps/api/staticmap?center=" +
  16.         latlon + "&zoom=14&size=400x300&sensor=false&key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU";
  17.     mapHolder.html("<img src='" + img_url + "'>");
  18. }
  19.  
  20. function showError(error) {
  21.     switch (error.code) {
  22.         case error.PERMISSION_DENIED:
  23.             mapHolder.html("User denied the request for Geolocation.");
  24.             break;
  25.         case error.POSITION_UNAVAILABLE:
  26.             mapHolder.html("Location information is unavailable.");
  27.             break;
  28.         case error.TIMEOUT:
  29.             mapHolder.html("The request to get user location timed out.")
  30.             break;
  31.         case error.UNKNOWN_ERROR:
  32.             mapHolder.html("An unknown error occurred.");
  33.             break;
  34.     }
  35. }
  36.  
  37. locationTag.on("click", () => {
  38.     promise
  39.         .then(showPosition,
  40.             showError);
  41. });
Advertisement
Add Comment
Please, Sign In to add comment