www.webdeveloper.com
Results 1 to 4 of 4

Thread: Return Value Problem

  1. #1
    Join Date
    Mar 2013
    Posts
    61

    Return Value Problem

    I'm trying to develop a web page that finds the users position and displays it on a map. I can find the position, but for some reason I always get the same error.

    06-04 14:04:19.186: E/Web Console(6630): Uncaught TypeError: Cannot read property 'LatLng' of undefined at file:///android_asset/www/GPSTraker.html:44
    Code:
    function initialize()
    {
    	x = document.getElementById("trackLocs");
    	//getLocation();
    	latlng = new google.maps.LatLng(0.0, 0.0);
    	loadMap(latlng);
    	if (navigator.geolocation)
        {		
        	navigator.geolocation.getCurrentPosition(showPosition);
        }
    	else  
    	{
    		x.innerHTML="Geolocation is not supported by this browser.";
    	}
    }
    
    function showPosition(position)
    {	
    	x.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
    	latlng = new google.map.LatLng( position.coords.latitude,  position.coords.longitude);
    	
    	return latlng;
    	
    }
    
    function loadMap(latlng)
    {		
    	var mapProp = { center:latlng, zoom:5,  mapTypeId:google.maps.MapTypeId.ROADMAP };		
    	var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);	
    		
    	var marker = new google.maps.Marker({
        				position: latlng, 
       					map: map,
        				title:"Your Location"
    					});
    }
    What I want to do is return the value of latlng, but instead I get the above error. Any ideas? Also Line 44 is the line in bold.

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    You're referencing: google.map which I suspect is undefined.

  3. #3
    Join Date
    Mar 2013
    Posts
    61
    Without any way that I know to get around that I've edited the script to get only what I really want. The lng and lan position values. So I've changed the showPosition() to return an array containing these values. For some reason this does not work, but the array is populated within the function. Any ideas?

    Code:
    function initialize()
    {
    	x = document.getElementById("trackLocs");
    	//getLocation();
    	locations = new Array();	
    	
    	if (navigator.geolocation)
        {		
        	navigator.geolocation.getCurrentPosition(showPosition);
    		latlng = new google.maps.LatLng(locations[0], locations[1]);
    		loadMap(latlng);
        }
    	else  
    	{
    		x.innerHTML="Geolocation is not supported by this browser.";
    	}
    }
    
    function showPosition(position)
    {	
    	locations = [position.coords.latitude, position.coords.longitude];
    	x.innerHTML="Latitude: " + locations[0] + "<br>Longitude: " + locations[1];
    	
    	return locations;
    	
    }

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by KarneeKarnay View Post
    Without any way that I know to get around that I've edited the script to get only what I really want.
    I'm not sure that you understood my answer.

    You had
    Code:
    latlng = new google.map.LatLng( position.coords.latitude,  position.coords.longitude);
    When I think you meant to write:
    Code:
    latlng = new google.maps.LatLng( position.coords.latitude,  position.coords.longitude);
    Last edited by Logic Ali; 06-05-2013 at 10:35 AM.

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