Repository navigation
Expand file tree
/
Copy path22.01.getmap.js
More file actions
37 lines (32 loc) · 1.76 KB
/
Copy path22.01.getmap.js
File metadata and controls
37 lines (32 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Return a newly created <img> element that will (once geolocation succeeds)
// be set to display a Google map of the current location. Note that the caller
// must insert the returned element into the document in order to make it
// visible. Throws an error if geolocation is not supported in the browser
function getmap() {
// Check for geolocation support
if (!navigator.geolocation) throw "Geolocation not supported";
// Create a new <img> element, start a geolocation request to make the img
// display a map of where we are, and then return the image.
var image = document.createElement("img");
navigator.geolocation.getCurrentPosition(setMapURL);
return image;
// This function will be invoked after we return the image object, when
// (and if) the geolocation request succeeds.
function setMapURL(pos) {
// Get our position information from the argument object
var latitude = pos.coords.latitude; // Degrees N of equator
var longitude = pos.coords.longitude; // Degrees E of Greenwich
var accuracy = pos.coords.accuracy; // Meters
// Construct a URL for a static Google map image of this location
var url = "http://maps.google.com/maps/api/staticmap" +
"?center=" + latitude + "," + longitude +
"&size=640x640&sensor=true";
// Set the map zoom level using a rough heuristic
var zoomlevel=20; // Start zoomed in almost all the way
if (accuracy > 80) // Zoom out for less accurate positions
zoomlevel -= Math.round(Math.log(accuracy/50)/Math.LN2);
url += "&zoom=" + zoomlevel; // Add zoom level to the URL
// Now display the map in the image object. Thanks, Google!
image.src = url;
}
}