2 Replies - 4560 Views - Last Post: 27 May 2010 - 04:38 PM

#1 tommod   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 32
  • Joined: 21-July 08

Problem with GMap and Global Variables

Posted 16 April 2010 - 03:49 AM

I am having a problem with some javascript. The problem is that I have a global gmap variable which is initialized in the init function which is called in the onload event in body. The functions showLocation and showPoint both should add a marker to the map. However, the gmap variable is 'undefined' in the scope of the function showPoint. showLocation on the other hand works fine. I am quite confused!

Hope someone can help.

<script type="text/javascript">
			var geocoder = new GClientGeocoder();
			var gmap;
	        function init() {
	        	// Initialise Google Map
				gmap = new GMap2(document.getElementById("map_canvas"));
	            gmap.setCenter(new GLatLng(54.9, -4.4), 6);
	        function showPoint(lat, lon) {
				// Show lat,long on map
				var point = new GLatLng(lat, lon);
				gmap.addOverlay(new GMarker(point));
	        function showLocation(loc) {
	        	// Show address on map
					function(point) {
						if (!point) {
							alert(loc + " not found");
						} else {
							gmap.addOverlay(new GMarker(point));

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with GMap and Global Variables

#2 Guest_Alfredo*


Re: Problem with GMap and Global Variables

Posted 06 May 2010 - 11:48 AM

Hi. Hope this could help

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="http://www.google.com/jsapi?key=yourGoogleMapsKey"></script>
<script type="text/javascript">

var MiLatitud = 54.9;
var MiLongitud = -4.4;
google.load("maps", "2");

function InicializaGoogleMaps() {
MiMapa = new GMap2(document.getElementById("MapaHTML"));
MiMapa.addControl(new GLargeMapControl());
MiMapa.addControl(new GMapTypeControl());
MiMapa.setCenter(new GLatLng( MiLatitud, MiLongitud), 6);
MiMapa.setMapType(G_HYBRID_MAP); // other gmaps types: G_NORMAL_MAP , G_SATELLITE_MAP , and G_HYBRID_MAP

var MiMarcador = new GLatLng(MiLatitud, MiLongitud);
MiMapa.addOverlay(new GMarker(MiMarcador));

<div id='MapaHTML' style='width: 750px; height: 600px'></div>

Take a look at http://www.besana111...public/abc.html
Good luck
Was This Post Helpful? 0

#3 Guest_grit*


Re: Problem with GMap and Global Variables

Posted 27 May 2010 - 04:38 PM

Hey tommod,

I just had exactly the same problem, that my javascript function to add markers on a map which is defined globally and initialized in my onload function as an instance of GMap2 could not access that map.

I could solve this problem by just inserting a timeout before adding the markers. I am not sure if this maybe occures because the map was not completely initialized at the point when I wanted to add a marker via the other function. However, here is my code in my javascript file, which works for me:

<script type="text/javascript">

var map;

function initialize() {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(52.478010, 13.423734), 15);

map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());


function setMarker(lat,lng) {
window.setTimeout(function() {
map.addOverlay(new GMarker(new GLatLng(lat,lng)));
}, 1000);

Was This Post Helpful? 0

Page 1 of 1