Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:04:19+00:00 2026-06-18T04:04:19+00:00

I have the following function that checks if the browser supports geo-location and then

  • 0

I have the following function that checks if the browser supports geo-location and then gets the users geo-location and centres it on the map.

What do I need to add to allow me to give the users direction to a fixed location (this won’t change) from the users geo-location?

if (navigator.geolocation)
{
  navigator.geolocation.getCurrentPosition(function(position)
  {                                                              
    var latitude = position.coords.latitude;                    
    var longitude = position.coords.longitude;                 
    var coords = new google.maps.LatLng(latitude, longitude);
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var mapOptions = 
    {
      zoom: 15,  
      center: coords, 
      mapTypeControl: true, 
      navigationControlOptions:
      {
        style: google.maps.NavigationControlStyle.SMALL
      },
       mapTypeId: google.maps.MapTypeId.ROADMAP
    };
     map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
     var marker = new google.maps.Marker( 
       {
         position: coords, 
         map: map,                 
        });
   });
}
else
{
   alert("Geolocation API is not supported in your browser.");

I’ve added this function to my code:

  function calcRoute() {
    var start = position;
    var end = "London";
    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);
      }
    });
  }

HTML:
<div id="mapContainer" onload="calcRoute()"></div>

But it still does not seem to be working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:04:21+00:00Added an answer on June 18, 2026 at 4:04 am

    Got it working with the following code:

    if (navigator.geolocation) { //Checks if browser supports geolocation
       navigator.geolocation.getCurrentPosition(function (position) {                                                              //This gets the
         var latitude = position.coords.latitude;                    //users current
         var longitude = position.coords.longitude;                 //location
         var coords = new google.maps.LatLng(latitude, longitude); //Creates variable for map coordinates
         var directionsService = new google.maps.DirectionsService();
         var directionsDisplay = new google.maps.DirectionsRenderer();
         var mapOptions = //Sets map options
         {
           zoom: 15,  //Sets zoom level (0-21)
           center: coords, //zoom in on users location
           mapTypeControl: true, //allows you to select map type eg. map or satellite
           navigationControlOptions:
           {
             style: google.maps.NavigationControlStyle.SMALL //sets map controls size eg. zoom
           },
           mapTypeId: google.maps.MapTypeId.ROADMAP //sets type of map Options:ROADMAP, SATELLITE, HYBRID, TERRIAN
         };
         map = new google.maps.Map( /*creates Map variable*/ document.getElementById("map"), mapOptions /*Creates a new map using the passed optional parameters in the mapOptions parameter.*/);
         directionsDisplay.setMap(map);
         directionsDisplay.setPanel(document.getElementById('panel'));
         var request = {
           origin: coords,
           destination: 'BT42 1FL',
           travelMode: google.maps.DirectionsTravelMode.DRIVING
         };
    
         directionsService.route(request, function (response, status) {
           if (status == google.maps.DirectionsStatus.OK) {
             directionsDisplay.setDirections(response);
           }
         });
       });
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function that loops through a directory and checks for a
I have the following function that checks for a user login. At it's current
I have the following function that works properly in Chrome and Firefox, but not
I have the following function that does string splitting: on splitText(aString, delimiter) set retVal
I have the following function that deletes the LaTeX command surrounding the current cursor
I have the following function that I am using to remove the characters \04
I have the following function that takes a number like 5 and creates a
I have the following function that retrieves an image from a twitter feed, the
I have the following function that is supposed to trigger anytime one of the
I have the following function that I only want to run inside <div id=imgWrapper>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.