Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Problem with Geolocation

Problem with Geolocation

Scheduled Pinned Locked Moved JavaScript
helpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14637431
    wrote on last edited by
    #1

    This is the first time i've posted on this website, so if i'm doing something wrong just let me know. Anyways, I'm having a problem getting my geolocation to work. I absolutely cannot figure out what the problem is. Without the geolocation bit of code, the default location, markers, and such work fine. Then when I try to add in the geolocation the map won't appear at all and just the button appears.

    var map;
    function initMap() {
    var myLatLng = {lat: 41.413118, lng: -82.072537}
    
     map = new google.maps.Map(document.getElementById('map'), {
      center: myLatLng,
      zoom: 18
     });
    
     var marker = new google.maps.Marker({
     position: myLatLng,
     map: map,
     title: 'My Location'
     });
    
     var marker = new google.maps.Marker({
     position: {lat: 41.412726, lng: -82.072047},
     map: map,
     title: 'PS Building',
     label: {text:'PS Building'}
     });
    
     infoWindow = new google.maps.InfoWindow;
    
     if (navigator.geolocation) {
     navigator.geolocation.getCurrentPosition(function(position) {
     var pos = {
       lat: position.coords.latitude,
       lng: position.coords.longitude
     };
     var marker = new google.maps.Marker({
      position: pos,
      map: map,
      title: 'Your Location'
     });
    
     infoWindow.setPosition(pos);
     infoWindow.open(map);
     map.setCenter(pos);
     }, function() {
     handleLocationError(true, infoWindow, map.getCenter());
     });
     } else {
     // Browser doesn't support Geolocation
     handleLocationError(false, infoWindow, map.getCenter());
    }
    

    }

    function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
    'Error: The Geolocation service failed.' :
    'Error: Your browser doesn\'t support
    geolocation.');
    infoWindow.open(map);
    }
    }

    N A 2 Replies Last reply
    0
    • M Member_14637431

      This is the first time i've posted on this website, so if i'm doing something wrong just let me know. Anyways, I'm having a problem getting my geolocation to work. I absolutely cannot figure out what the problem is. Without the geolocation bit of code, the default location, markers, and such work fine. Then when I try to add in the geolocation the map won't appear at all and just the button appears.

      var map;
      function initMap() {
      var myLatLng = {lat: 41.413118, lng: -82.072537}
      
       map = new google.maps.Map(document.getElementById('map'), {
        center: myLatLng,
        zoom: 18
       });
      
       var marker = new google.maps.Marker({
       position: myLatLng,
       map: map,
       title: 'My Location'
       });
      
       var marker = new google.maps.Marker({
       position: {lat: 41.412726, lng: -82.072047},
       map: map,
       title: 'PS Building',
       label: {text:'PS Building'}
       });
      
       infoWindow = new google.maps.InfoWindow;
      
       if (navigator.geolocation) {
       navigator.geolocation.getCurrentPosition(function(position) {
       var pos = {
         lat: position.coords.latitude,
         lng: position.coords.longitude
       };
       var marker = new google.maps.Marker({
        position: pos,
        map: map,
        title: 'Your Location'
       });
      
       infoWindow.setPosition(pos);
       infoWindow.open(map);
       map.setCenter(pos);
       }, function() {
       handleLocationError(true, infoWindow, map.getCenter());
       });
       } else {
       // Browser doesn't support Geolocation
       handleLocationError(false, infoWindow, map.getCenter());
      }
      

      }

      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
      infoWindow.setPosition(pos);
      infoWindow.setContent(browserHasGeolocation ?
      'Error: The Geolocation service failed.' :
      'Error: Your browser doesn\'t support
      geolocation.');
      infoWindow.open(map);
      }
      }

      N Offline
      N Offline
      Nathan Minier
      wrote on last edited by
      #2

      One glaring issue:

      infoWindow = new google.maps.InfoWindow;

      You never declare the variable, and you need the constructor when calling it, so...

      var infoWindow = new google.maps.InfoWindow();

      All of this should be visible as errors in your javascript console. Are you not using browser Dev tools to debug? Edit: Stupids

      "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

      Richard DeemingR 1 Reply Last reply
      0
      • N Nathan Minier

        One glaring issue:

        infoWindow = new google.maps.InfoWindow;

        You never declare the variable, and you need the constructor when calling it, so...

        var infoWindow = new google.maps.InfoWindow();

        All of this should be visible as errors in your javascript console. Are you not using browser Dev tools to debug? Edit: Stupids

        "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Javascript doesn't require that variables are declared before they're used, unless you enable "strict" mode[^]. It also doesn't always require the parentheses when calling a constructor. For example, foo = new Date; seems to work fine without any errors or warnings. Basically, Javascript is the VB6 of the web. :)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        N 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Javascript doesn't require that variables are declared before they're used, unless you enable "strict" mode[^]. It also doesn't always require the parentheses when calling a constructor. For example, foo = new Date; seems to work fine without any errors or warnings. Basically, Javascript is the VB6 of the web. :)


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          N Offline
          N Offline
          Nathan Minier
          wrote on last edited by
          #4

          Haven't worked outside of strict mode in ages, forgot about that bunk.

          "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

          1 Reply Last reply
          0
          • M Member_14637431

            This is the first time i've posted on this website, so if i'm doing something wrong just let me know. Anyways, I'm having a problem getting my geolocation to work. I absolutely cannot figure out what the problem is. Without the geolocation bit of code, the default location, markers, and such work fine. Then when I try to add in the geolocation the map won't appear at all and just the button appears.

            var map;
            function initMap() {
            var myLatLng = {lat: 41.413118, lng: -82.072537}
            
             map = new google.maps.Map(document.getElementById('map'), {
              center: myLatLng,
              zoom: 18
             });
            
             var marker = new google.maps.Marker({
             position: myLatLng,
             map: map,
             title: 'My Location'
             });
            
             var marker = new google.maps.Marker({
             position: {lat: 41.412726, lng: -82.072047},
             map: map,
             title: 'PS Building',
             label: {text:'PS Building'}
             });
            
             infoWindow = new google.maps.InfoWindow;
            
             if (navigator.geolocation) {
             navigator.geolocation.getCurrentPosition(function(position) {
             var pos = {
               lat: position.coords.latitude,
               lng: position.coords.longitude
             };
             var marker = new google.maps.Marker({
              position: pos,
              map: map,
              title: 'Your Location'
             });
            
             infoWindow.setPosition(pos);
             infoWindow.open(map);
             map.setCenter(pos);
             }, function() {
             handleLocationError(true, infoWindow, map.getCenter());
             });
             } else {
             // Browser doesn't support Geolocation
             handleLocationError(false, infoWindow, map.getCenter());
            }
            

            }

            function handleLocationError(browserHasGeolocation, infoWindow, pos) {
            infoWindow.setPosition(pos);
            infoWindow.setContent(browserHasGeolocation ?
            'Error: The Geolocation service failed.' :
            'Error: Your browser doesn\'t support
            geolocation.');
            infoWindow.open(map);
            }
            }

            A Offline
            A Offline
            A_Griffin
            wrote on last edited by
            #5

            Your very final closing curly } is extraneous - just get rid of that and your code will work. btwm, you ought to hide yor API key - or at least go to your Gogole account and restrict its use to your IP address. As it is, anyone can use it....

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups