Can’t get current location

Can’t get current location

BY 21 Apr 2015 Web Application Development

Hi,

I have tried all the ways I could get my hands on for getting the current location but I am unable to make it work. I am using a development purpose device for testing this. I want to get the current location (which is working fine in web simulator application) and feed it to some function which draws the route on a google map. In the target device, if I feed two locations( i.e. fromLocation and  toLocation) manually , it works fine but as soon as I give ‘fromLocation’ as the current location, nothing is shown in the map div. The code is as follows :

//inside some function
if(navigator.geolocation) {
    	console.log("inside geolocation function");
	    navigator.geolocation.getCurrentPosition(function(position) {	
	      var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);//It gets stuck at this statement
	      console.log(pos);
	      calculateRoute(pos,destination);
	      });
	      }
	else console.log("does not support geolocation");
	
    
//calculateRoute function
function calculateRoute(from, to) {
      // Center initialized to New Delhi, India
      var myOptions = {
        zoom: 10,
        center: new google.maps.LatLng(28.6, 77.2),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        streetViewControl : false
      };
      // Draw the map
      var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);

      var directionsService = new google.maps.DirectionsService();
      
      var directionsRequest = {
        origin: from,
        destination: to,
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC
      };
      directionsService.route(
        directionsRequest,
        function(response, status)
        {
          if (status == google.maps.DirectionsStatus.OK)
          {
            new google.maps.DirectionsRenderer({
              map: mapObject,
              directions: response
            });
          }
          
        }
      );
    }

Here, if instead of

calculateRoute(pos,destination);

I give something like –

calculateRoute("Bangalore","Delhi");

then it works. So, the problem lies with finding the current location. I have given the location priveleges also in the config file.

Please help

Thanks.
 

Written by