www.webdeveloper.com
Results 1 to 3 of 3

Thread: Load function help

  1. #1
    Join Date
    Mar 2013
    Posts
    61

    Load function help

    I'm trying to design a web page that gets the users position and displays it on google maps. I'm trying to get the getLocation() to load at the start with the map. For some reason I doesn't. Here is a link to where I found the get location code. http://www.w3schools.com/html/tryit....l5_geolocation

    Code:
    script
    src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAc3TUzM5iM79TeE5JBYi1nYRcKW2HjCec&sensor=false">
    </script>
    
    <script>
    
    //var locations = new Array();
    
    var displayLoc = document.getElementById("trackLocs");
    
    //get positions
    function getLocation()
     { 	 
      if (navigator.geolocation)
       {
       	navigator.geolocation.getCurrentPosition(showPosition);
       }	
      else{displayLoc.innerHTML="Geolocation is not supported by this browser.";}
    }
      
    function showPosition(position)
    {
      displayLoc.innerHTML="Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude;	
    }
    
    //load map
    function initialize()
    {
    var mapProp = {
      center:new google.maps.LatLng(51.508742,-0.120850),
      zoom:5,
      mapTypeId:google.maps.MapTypeId.ROADMAP
      };
    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    window.onload = getLocation();
    </script>
    
    </head>
    
    <body>
    <div class="span-24 last" id="googleMap" style="width:100%; height: 380px;"></div>
    
    <div class="span-24 last" id="trackLocs"> </div>
    </body>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    try window.onload = getLocation; instead of window.onload = getLocation();
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by Padonak View Post
    try window.onload = getLocation; instead of window.onload = getLocation();
    That worked, but the problem stays. It still fails to display the data. I don't know what it is, but I get a bug when I try running it through an eclipse editor.

    06-01 17:00:41.078: E/Web Console(8061): Uncaught TypeError: Cannot set property 'innerHTML' of null at file:///android_asset/www/GPSTraker.html:35
    The other strange thing is this.

    Code:
     if (navigator.geolocation)
       {
            alert("Fired");
       	navigator.geolocation.getCurrentPosition(showPosition);
       }
    The alert is not fired for some reason. The function is called (I tested that), but for some reason the if statement is never fired. That leads me to belive that this problem is linked an innerHTML issue and a Navigator one.

    EDIT: Defiantly a problem with the innerHTML. Tested the else that fired. This means there is a nav problem as well.

    EDIT2: Solved both. Had to get element within function, and Chrome was responsible for the lack of data. :P
    Last edited by KarneeKarnay; 06-01-2013 at 02:19 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles