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 6892911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:35:35+00:00 2026-05-27T06:35:35+00:00

I have the following Javascript where I modified the standard Google Maps API initialize()

  • 0

I have the following Javascript where I modified the standard Google Maps API initialize() function and the standard addMarker function. The map loads and centers fine, but the marker does not get added to the map. I looked at the previous question , but I’m not sure what I’m doing wrong..

<script type="text/javascript">
// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js

var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 
var usa = new google.maps.LatLng(44.58, -95.46); //middle of the US w 50 states     

var browserSupportFlag =  new Boolean();

function initialize() {
    var myOptions = {
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
  map.setCenter(usa);   
}

function getMyLocation() {
    var initialLocation= newyork;
    var myOptions = {
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    // Try W3C Geolocation (Preferred)
    if(navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function(position) {
        initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
        map.setCenter(initialLocation);
        addMarker(initialLocation);
    }, function() {
    handleNoGeolocation(browserSupportFlag);
    });
    // Try Google Gears Geolocation
    } else if (google.gears) {
        browserSupportFlag = true;
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function(position) {
        initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
        map.setCenter(initialLocation);
        addMarker(initialLocation);
    }, function() {
        handleNoGeoLocation(browserSupportFlag);
    });
    // Browser doesn't support Geolocation
    } else {
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }

    function handleNoGeolocation(errorFlag) {
        if (errorFlag == true) {
            alert("Geolocation service failed.");
            initialLocation = newyork;
        } else {
             alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
             initialLocation = siberia;
        }
        map.setCenter(usa);
    }

}

function addMarker(location) {
    marker = new google.maps.Marker({
    position: location,
    map: map, 
    title: "Hello World!"
    });
    //markersArray.push(marker);
}
</script>
  • 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-05-27T06:35:36+00:00Added an answer on May 27, 2026 at 6:35 am

    So your problem is to do with scope:

    Add this above your initilize function:

    var map;
    

    And in your initialize function change this:

      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
    

    to this:

      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
    

    You don’t need to re-create the map in your getlocation function:

    so remove these lines:

       var myOptions = {
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    

    Here is a jsfiddle with the changes:

    http://jsfiddle.net/bQ3AG/

    EDIT: I’ve just noticed when you pin point their location, you want to set the zoom level to 10.

    To do this you can use: map.setZoom(10); instead of passing through new options.

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

Sidebar

Related Questions

I have the following javascript code, which loads without error, however the update function
I have the following javascript: function graphStreamPublish(){ FB.api('/me/feed', 'post', { message : 1, link
I have the following JavaScript code: Link In which the function makewindows does not
I have the following Javascript function that makes a call to an Asp.Net WebMethod
I have the following JavaScript code: var cILo=true; var img1=images/title-2a.png; var img2=images/title-2b.png; function loadblinker()
I have the following javascript : var MyObject = (function() { function Setup(args) {
I have the following javascript code, which is modified from the SimpleUpload demo in
I have the following JavaScript (and jQuery) code: function checkEmail(email) { if (email.length) {
I have the following javascript on my page: var isDirty = false; function OnTextBoxValueChanged(sender,
I have the following javascript function to validate the usercode value entered in the

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.