-1

I have adapted this code to try and get it to work for my situation. What I am attempting to do is find the visitors current location, and map directions to a certain location on load.

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">  
</script>

<script>
  var directionsDisplay;
  var directionsService = new google.maps.DirectionsService();

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var mapOptions = {
        zoom: 7,
        center: new google.maps.LatLng(38.3094610,-85.5791560)
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
    var control = document.getElementById('control');
    control.style.display = 'block';
    map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
  }

  function calcRoute() {
    var start = 'current location';
    var end = ('38.3094610,-85.5791560');
    var request = {
        origin:start,
        destination:end,
        travelMode: google.maps.TravelMode.DRIVING
     };

    directionsService.route(request, function(result, status) {
       if (status == google.maps.DirectionsStatus.OK) {
           directionsDisplay.setDirections(result);
       }
    });
  }

  google.maps.event.addDomListener(window, 'load', initialize);

</script>

What I'm needing is to store the current location in a variable so I can use it in the calcRoute function as start.

ivpavici
  • 1,117
  • 2
  • 19
  • 30
  • Welcome to Stack Overflow. When asking a question, you should really try to demonstrate that you have made some effort to so solve the problem and researched it thoroughly. You can't just dump a load of code and expect people to fix it for you. See here for guidelines on asking good questions http://stackoverflow.com/questions/how-to-ask – Martin Davies Jan 20 '14 at 00:42
  • Not trying to be rude here but I said all that in my first line.. "I have adapted this code to try and get it to work for my situation" Adapted meaning that I have altered/changed/tried to get it to work. Also, I have tried researching this, how do you think I came accross this site? – user3213379 Jan 20 '14 at 02:10
  • All questions from new users go through a review process to ensure they meet the guidelines mentioned above, and to make sure you are aware of their importance. In this case I felt that your question could be improved by some of the suggestions in that article. It is of course up to you if you choose to do so. – Martin Davies Jan 20 '14 at 02:34

2 Answers2

1

First detect whether the browser supports geo tracking at all:

if (!window.navigator||
    !window.navigator.geolocation||
    !window.navigator.geolocation.watchPosition) return;

Then build location handlers:

function geo_success(pos){
  var lat=pos.coords.latitude;
  var lng=pos.coords.longitude;

  //do your mapping magic here
}

function geo_error(pos){
  // do nothing
}

Now register the tracker:

navigator.geolocation.watchPosition(
    geo_success, geo_error, 
    {enableHighAccuracy:true, maximumAge:10000, timeout:10000});
Schien
  • 3,855
  • 1
  • 16
  • 29
0

Are you asking how to get the gelocation from the browser? If so, try this article

TL;DR

navigator.geolocation.getCurrentPosition(callback);

where callback is a javascript function to call after the user's location has been determined.

Mike
  • 1,625
  • 3
  • 16
  • 19
  • I don't see in the code where it is obtained, and I have searched for examples on how to obtain them but I am having a hard time implementing them. Every change that I make results in a partial page load. This appears to be the easiest yet I can't implement it lol var lat=pos.coords.latitude; var lng=pos.coords.longitude; @Mike – user3213379 Jan 21 '14 at 02:23