What am I missing here?

35 views
Skip to first unread message

Dan

unread,
Jan 1, 2012, 7:09:17 PM1/1/12
to google-map...@googlegroups.com
Hi, after the page has allready loaded I'm trying to get another javascript function to add a marker on the loaded map. The below is a short version of what I'm trying to do based on the API examples, but it does not add the marker... what am I missing? Any ideas?

thanks in advance

<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Marker Simple</title>

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  
    function callafunction(){

     var latlng = new google.maps.LatLng(-25.363882,131.044922);
    marker = new google.maps.Marker({
      position: latlng,
      map: map
    });
  
    }
  
  function initialize() {
    var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
    var myOptions = {
      zoom: 4,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  }
</script>
</head>
<body onload="initialize()">
        <form id="callafunction" action="#" onSubmit="return false;">
                    <input type="submit" value="callafunction" onClick="callafunction(); return false;"/>
        </form>
  <div id="map_canvas"></div>
</body>
</html>

Marcelo

unread,
Jan 1, 2012, 10:59:32 PM1/1/12
to Google Maps JavaScript API v3
On Jan 1, 5:09 pm, Dan <dan....@gmail.com> wrote:
> Hi, after the page has allready loaded I'm trying to get another javascript
> function to add a marker on the loaded map. The below is a short version of
> what I'm trying to do based on the API examples, but it does not add the
> marker... what am I missing? Any ideas?

You're missing a link to a page that demonstrates the problem.

--
Marcelo - http://maps.forum.nu
--





>
> thanks in advance
>
> <!DOCTYPE html>
> <html>
> <head>
> <title>Google Maps JavaScript API v3 Example: Marker Simple</title>
>
> <link
> href="http://code.google.com/apis/maps/documentation/javascript/examples/de..."

Alan Pearce

unread,
Jan 2, 2012, 12:48:55 AM1/2/12
to google-map...@googlegroups.com
The map variable is declared in the initialize function so it is not available in the callafunction function.

Dan

unread,
Jan 2, 2012, 1:19:16 AM1/2/12
to Google Maps JavaScript API v3
sorry, my bad. here it is:
http://utopic.nfshost.com/newmapcanvas.html


On Jan 1, 7:59 pm, Marcelo <marcelo...@hotmail.com> wrote:
> On Jan 1, 5:09 pm, Dan <dan....@gmail.com> wrote:
>
> > Hi, after the page has allready loaded I'm trying to get another javascript
> > function to add a marker on the loaded map. The below is a short version of
> > what I'm trying to do based on the API examples, but it does not add the
> > marker... what am I missing? Any ideas?
>
> You're missing a link to a page that demonstrates the problem.
>
> --
> Marcelo -http://maps.forum.nu
> > </html>- Hide quoted text -
>
> - Show quoted text -

Rossko

unread,
Jan 2, 2012, 7:12:05 AM1/2/12
to Google Maps JavaScript API v3
> sorry, my bad. here it is:http://utopic.nfshost.com/newmapcanvas.html

I get a straightforward javascript error upon clicking the button,
'map' is not defined.
Your 'map' variable was defined locally in your initialize() function,
so that it is not available later in the global scope that button-
click runs in.
Make your 'map' variable global, you can still set it up as a map
object within initialize()

It's worth finding out how to see the javascript errors your browser
reports
Reply all
Reply to author
Forward
0 new messages