谷歌地图泛到

use*_*952 5 javascript google-maps pan

从文本链接设置平移到谷歌地图一整天都在苦苦挣扎.看起来地图本身的id并没有被传递给函数,但是我尝试了许多没有快乐的东西.

<script type="text/javascript">
    var home = new google.maps.LatLng(4.915833, 10.195313);
    function initialize() {
        var myOptions = {
            zoom: 2,
            center: home,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false,
            panControl: true,
            panControlOptions: {
                position: google.maps.ControlPosition.LEFT_CENTER
            },
            zoomControl: true,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.LARGE,
                position: google.maps.ControlPosition.LEFT_CENTER
            },
            scaleControl: false,
            streetViewControl: true,
            streetViewControlOptions: {
                position: google.maps.ControlPosition.LEFT_CENTER
            }
        }
        var map = new google.maps.Map(document.getElementById("map"), myOptions);
        setMarkers(map, destinations);
        clickroute(map);
    }
    var destinations = [
        ['Marbella', 36.509937, -4.886352],
        ['Algarve', 37.016945, -7.928728],
        ['London', 51.508129, -0.128005],
        ['Istanbul', 41.00527, 28.97696],
        ['Whistler', 50.116168, -122.959423]
    ];
    function setMarkers(map, locations) {
        var image = new google.maps.MarkerImage('img/marker.png',
        new google.maps.Size(41, 63),
        new google.maps.Point(0,0));
        for (var i = 0; i < locations.length; i++) {
            var destination = locations[i];
            var myLatLng = new google.maps.LatLng(destination[1], destination[2]);
            var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                icon: image,
                title: destination[0]
            });
        }
    }
    function clickroute(lati, long) {
        var latLng = new google.maps.LatLng(51.433373, -0.712251);
        map.panTo(latLng);
    }
</script> 

<li onclick="clickroute()">test</li>
Run Code Online (Sandbox Code Playgroud)

关于什么可能导致问题的任何可能的想法?我得到一个js错误

Uncaught TypeError: Object #<HTMLDivElement> has no method 'panTo'
Run Code Online (Sandbox Code Playgroud)

谢谢

理查德

tus*_*sar 15

@onemach是对的.

1.你必须map在javascript的开头声明为全局变量.只需var map;<script type="text/javascript">标记后立即做声明.

2.同时你的地图初始化应改为

map = new google.maps.Map(document.getElementById("map"), myOptions); // without the 'var'
Run Code Online (Sandbox Code Playgroud)

clickroute()<li>没有参数的情况下调用onClick .所以改变这样的定义 clickroute():

function clickroute() { //just omit the 'lati' and 'long'
    var latLng = new google.maps.LatLng(51.433373, -0.712251);
    map.panTo(latLng);
}
Run Code Online (Sandbox Code Playgroud)

现在点击test <li>你的地图将移动到点(51.433373,-0.712251)