Created
August 27, 2013 19:23
-
-
Save daviosa/6357883 to your computer and use it in GitHub Desktop.
Javascript functions to get user location and plot it on a googlemaps canvas. HTML5 + Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function success(position) { | |
var s = document.querySelector('#status'); | |
if (s.className == 'success') { | |
return; | |
} | |
s.innerHTML = "Você foi localizado!"; | |
s.className = 'success'; | |
var mapcanvas = document.createElement('div'); | |
mapcanvas.id = 'mapcanvas'; | |
mapcanvas.style.height = '400px'; | |
mapcanvas.style.width = '560px'; | |
document.querySelector('article').appendChild(mapcanvas); | |
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); | |
var myOptions = { | |
zoom: 15, | |
center: latlng, | |
mapTypeControl: false, | |
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions); | |
var marker = new google.maps.Marker({ | |
position: latlng, | |
map: map, | |
title:"Você está aqui!" | |
}); | |
} | |
function error(msg) { | |
var s = document.querySelector('#status'); | |
s.innerHTML = typeof msg == 'string' ? msg : "falhou"; | |
s.className = 'fail'; | |
} | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(success, error); | |
} else { | |
error('Seu navegador não suporta <b style="color:black;background-color:#ffff66">Geolocalização</b>!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment