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
2 Posts 2 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);
    }
    }

    A 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
      #2

      Why are you repeating this question? It's been answered, below. (Though you may also need to put

                         'Error: Your browser doesn\\'t support 
      

      geolocation.');

      on one line first, if that's not just a formatting issue on this site.) And your

      onclick="GetLocation();"

      attached to the button won't do anything unless you define a function with that name in your script.

      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