Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Giovanni Giannola
Giovanni Giannola
1,119 Points

Get geolocation onclick issue (Using google maps api v3)

Im trying to build a map that shows the location when I click a button outside the google maps.

I am taking the map from the Google maps api v3 as the tell me to and I'm also able to get the user location with a maker and even the infowindow shows up but as soon as I add everything into a single function and try to call it from the HTML it doesn't work but the map keeps showing so I'm guessing the code doesn't have any errors. I'm probably missing something in my code.... .... .... I will leave my code here so maybe someone can help me out with this Thanks

Giovanni Giannola
Giovanni Giannola
1,119 Points
<!DOCTYPE html>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Disabling the default UI</title>
      html, body, #mymap{
        height: 100%;
        margin: 0px;
        padding: 0px
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>

 var map;

function initialize() {

var miami = new google.maps.LatLng(41.85, -87.65);

  var mapOptions = {
    zoom: 4,
    center: miami,
    disableDefaultUI: true,
  map = new google.maps.Map(document.getElementById('mymap'),

  var myloc = document.getElementById("try");

  function getlocation () {

    // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,

      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: pos,
        content: 'Location found using HTML5.'

    }, function() {
  } else {
    // Browser doesn't support Geolocation

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content

  var infowindow = new google.maps.InfoWindow(options);


google.maps.event.addDomListener(window, 'load', initialize);

    <div id="try"><button onclick="getlocation()">Click here</button></div>
    <div id="mymap"></div>