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

  • Home
  • SEARCH
  • 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 9230989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:57:28+00:00 2026-06-18T05:57:28+00:00

I’m pretty much a noob when it comes to javascript, but I wanted to

  • 0

I’m pretty much a noob when it comes to javascript, but I wanted to place a Google Map of my location on my website, and to have a simple form where visitors can enter their address and the directions would be shown. I’ve got it working OK, but it’s been mentioned that there are too many global variables created by the script. The globals are as follows:

calcRoute, directionsDisplay, directionsService, google, initialize, map

If I try to make any of these globals local instead, it breaks the script, and the map doesn’t display. Here’s the js I have written:

var initialize = function strict() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    mapCenter = new google.maps.LatLng(53.503716, -1.12975);
    mapOptions = {
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: mapCenter
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
    locationPin = "images/location_pin.png";
    newMarker = new google.maps.Marker({ position: mapCenter,map: map,icon: locationPin});
    };

var calcRoute = function strict() {
    routeStart = document.getElementById("start").value;
    calculatedRoute = {origin: routeStart, destination: mapCenter, travelMode: google.maps.TravelMode.DRIVING};
    directionsService.route(calculatedRoute, function (routeStart, mapCenter) {
    directionsDisplay.setDirections(routeStart);
    calculatedRoute = routeStart.routes[0].legs[0];
    });
};
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

As far as I can see, the globals I mentioned are required by the Google Maps API and they have to be globals. Any advice on this would be very gratefully welcomed.

  • 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-18T05:57:30+00:00Added an answer on June 18, 2026 at 5:57 am

    Your code is OK!

    But, if you don’t want that this variable be global, insert your code at a function, like:

    (function () { 
        //your code 
    }());
    

    Using this, the variables will be at a scope, but in your case, it dosen’t make difference…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm making a simple page using Google Maps API 3. My first. One marker
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.