views:

17

answers:

1

Hello guys,

I have created a very basic google map using the javascript API and google's tutorials. I would love to be able to create hyperlinks on my page that would recenter the map at certain cities around the world - eg London, Paris, Rome..

I can't for the life of me figure out how to do this (I'm quite new to all of this, trying to teach myself some code...).

Could anybody please offer some advice, or even link to an example (I could probebly figure it out from the source code.)

Appreciate your help!

-nac

A: 

Ok I figured it out - my problem was that the map object was a local variable, and I was trying to access it outside the function. All I needed to do was initialize the variable first from outside the loadMap function.

<head>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;
    <script type="text/javascript">        
        var map;
        function loadMap() {
            var latlng = new google.maps.LatLng(-25.363882,131.044922);
            var myOptions = {
                zoom: 16,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map"),
            myOptions);
        }

    function moveMap() {
        var darwin = new google.maps.LatLng(-12.461334, 130.841904);
        map.setCenter(darwin);

    }
    </script>
</head>
<body onload="loadMap()">
    <div id="map">
    </div>
<div id="controls">
    <a href="javascript:moveMap();">Move</a>
</div>      
</body>
noobatcode