Function help - can't make it work!

Discussion in 'JavaScript' started by aquasonic, Jun 20, 2008.

  1. #1
    I am trying to do a google map - and I'm fine with it... what I'm having problems with is controlling the map from links on the page.

    I want something to happen to the map when you mouseover a link on the page.

    I have the code in my website set up like this:
    <a href="#" onmouseover="doSomething();" onmouseout="return false;">test</a>
    Code (markup):
    And I have the map script (which is all working corectly) set up like this... my function is near the bottom... what am I doing wrong? I get the error:
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
            //map.addControl(new GMapTypeControl());
            //map.addControl(new GSmallZoomControl());
            map.setCenter(new GLatLng(52.28164125024719, -1.3841164112091064), 6);
            GDownloadUrl("depotlist.json", function( data, responseCode ){ parseJson(data); });
            map.setMapType(G_NORMAL_MAP);
    	map.disableDragging();
    
            function makeIcon (image) {
            var icon = new GIcon();
            icon.image = image;
            icon.iconSize = new GSize(27, 27);
            icon.shadowSize = new GSize(24, 16);
            icon.iconAnchor = new GPoint(13, 13);
            icon.infoShadowAnchor = new GPoint(0, 0);
            icon.infoWindowAnchor = new GPoint(8, 1);
            return icon;
            }
    
            function createMarker(input) { 
                  var marker = new PdMarker(input.point, makeIcon(input.icon) );
                  marker.setTooltip( input.depotName );
                  GEvent.addListener(marker, "click", function() {
    
            if (marker.getMouseOutEnabled())
            {
                    marker.setMouseOutEnabled(false);
                    map.setCenter((input.point), 17);
                    map.setMapType(G_SATELLITE_MAP);
                    document.getElementById('address').innerHTML = (input.address);
            }
            else
            {
                    marker.setMouseOutEnabled(true); 
                    map.setCenter(new GLatLng(52.28164125024719, -1.3841164112091064), 6);
                    map.setMapType(G_NORMAL_MAP);
                    document.getElementById('address').innerHTML = ('&nbsp;');
            }
            });
    
    	function doSomething() {
    		map.setMapType(G_SATELLITE_MAP);
    	} 
    
            return marker;
    
    
            }
    
            function parseJson (doc) {
                  var jsonData = eval("(" + doc + ")");
                    for (var i = 0; i< jsonData.depots.length; i++) {
                    var marker = createMarker(jsonData.depots[i]);
            map.addOverlay(marker); }
            }
    
    
      }
    
    }
    window.onload = initialize;
    window.onunload = GUnload;
    
    
    Code (markup):

     
    aquasonic, Jun 20, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Move the doSomething function outside of the initialize function.
     
    MMJ, Jun 20, 2008 IP
  3. aquasonic

    aquasonic Well-Known Member

    Messages:
    90
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    115
    #3
    Then it says

    ????
     
    aquasonic, Jun 20, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try:

    var map;
    function doSomething() {
    	map.setMapType(G_SATELLITE_MAP);
    }
    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
            //map.addControl(new GMapTypeControl());
            //map.addControl(new GSmallZoomControl());
            map.setCenter(new GLatLng(52.28164125024719, -1.3841164112091064), 6);
            GDownloadUrl("depotlist.json", function( data, responseCode ){ parseJson(data); });
            map.setMapType(G_NORMAL_MAP);
    	map.disableDragging();
    
            function makeIcon (image) {
            var icon = new GIcon();
            icon.image = image;
            icon.iconSize = new GSize(27, 27);
            icon.shadowSize = new GSize(24, 16);
            icon.iconAnchor = new GPoint(13, 13);
            icon.infoShadowAnchor = new GPoint(0, 0);
            icon.infoWindowAnchor = new GPoint(8, 1);
            return icon;
            }
    
            function createMarker(input) { 
                  var marker = new PdMarker(input.point, makeIcon(input.icon) );
                  marker.setTooltip( input.depotName );
                  GEvent.addListener(marker, "click", function() {
    
            if (marker.getMouseOutEnabled())
            {
                    marker.setMouseOutEnabled(false);
                    map.setCenter((input.point), 17);
                    map.setMapType(G_SATELLITE_MAP);
                    document.getElementById('address').innerHTML = (input.address);
            }
            else
            {
                    marker.setMouseOutEnabled(true); 
                    map.setCenter(new GLatLng(52.28164125024719, -1.3841164112091064), 6);
                    map.setMapType(G_NORMAL_MAP);
                    document.getElementById('address').innerHTML = ('&nbsp;');
            }
            });
    
    
            return marker;
    
    
            }
    
            function parseJson (doc) {
                  var jsonData = eval("(" + doc + ")");
                    for (var i = 0; i< jsonData.depots.length; i++) {
                    var marker = createMarker(jsonData.depots[i]);
            map.addOverlay(marker); }
            }
    
    
      }
    
    }
    window.onload = initialize;
    window.onunload = GUnload;
    PHP:
    Where did you get this code from?
     
    MMJ, Jun 20, 2008 IP
    aquasonic likes this.
  5. aquasonic

    aquasonic Well-Known Member

    Messages:
    90
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    115
    #5
    Ah! It makes sense when I see it written like that!

    Thank you, that's sorted all my problems.

    I wrote the code with the aid of the PDMarker user guide website - why?
     
    aquasonic, Jun 20, 2008 IP
  6. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No problem.

    Just wondering. :)
     
    MMJ, Jun 21, 2008 IP