map undefined error

1,504 views
Skip to first unread message

Water Geek

unread,
Jun 20, 2010, 6:02:34 PM6/20/10
to Google Maps JavaScript API v3
this is driving me crazy and I know it should be simple.

I do not currently have a server to post this on...but I'm workng on
it.

I just want to click a button and have an alert show the lat, long of
the map center. I keep getting an error that the 'map' variable is
not defined.

any thoughts?



<html>
<head>
<title></title>




<!-- Load Google Maps API -->
<script type="text/javascript" src="http://maps.google.com/maps/api/
js?sensor=false"></script>



<script type="text/javascript">
//Google Load
function GoogleLoadMap() {
try {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new
google.maps.Map(document.getElementById("MyMap"), myOptions);
}
catch (e) {
alert("Google Maps could not load (GoogleLoadMap Function
Error) : " + e.message);
}
}

//get map info for alert window
function GoogleMapInfo() {
try {

var center = map.getCenter();
var zoom = map.getZoom();
alert("Center: " + center + " Zoom: " + zoom);
}
catch (e) {
alert("Google Maps could not load (GoogleMapInfo Function
Error) : " + e.message);
}
}


</script>
</head>

<body onload="GoogleLoadMap()" bgcolor= "black">
<input type="button" value="Map Info?" onclick="GoogleMapInfo()"/
>

<div id="MyMap" style="width: 100%; height: 100%">
</div>




</body>
</html>

Ralph Ames

unread,
Jun 20, 2010, 7:54:43 PM6/20/10
to google-map...@googlegroups.com
Define the map variable in global scope

var map;
function GoogleLoadMap() {

and remove the var from this line


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


Ralph
-
www.easypagez.com/maps/map_index.html
www.easypagez.com/maps/v3_basicmap.html

--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to
google-maps-js-a...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Water Geek

unread,
Jun 21, 2010, 8:28:50 PM6/21/10
to Google Maps JavaScript API v3
Thanks! I feel like and idiot.

I actually had the map declared as a global variable before copying
and pasting into one html file for the post...but the main problem was
that I declared the variable twice, once as a global and once inside
the function.

Thanks for the debuging help, I would have caught that in scripting
languages that I know better than java.
Reply all
Reply to author
Forward
0 new messages