【问题标题】:google maps pan to谷歌地图平移到
【发布时间】:2012-04-10 02:25:45
【问题描述】:

整天都在努力通过文本链接在谷歌地图中设置平移。看起来地图本身的 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>

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

Uncaught TypeError: Object #<HTMLDivElement> has no method 'panTo'

谢谢

理查德

【问题讨论】:

    标签: javascript google-maps pan


    【解决方案1】:

    @onemach 是正确的。

    1。 您必须在 javascript 的开头将 map 声明为全局变量。只需在&lt;script type="text/javascript"&gt; 标记之后立即声明var map;

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

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

    3。您调用clickroute() onClick 的&lt;li&gt; 不带参数。所以像这样改变clickroute()的定义:

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

    现在点击test&lt;li&gt;,您的地图将移动到点(51.433373,-0.712251)

    【讨论】:

      【解决方案2】:

      clickroute(map);与定义不兼容

      function clickroute(lati, long) {
          var latLng = new google.maps.LatLng(51.433373, -0.712251);
          map.panTo(latLng);
      }
      

      我猜你想要什么

      function clickroute(map) {
          var latLng = new google.maps.LatLng(51.433373, -0.712251);
          map.panTo(latLng);
      }
      

      更新:

      我很困惑你到底想要什么。

      onclick=clickroute() 不向函数传递任何参数。

      在clickroute中也没有全局map

      【讨论】:

      • 感谢您抽出宝贵的时间回复,但仍然对此感到不满意,同样的错误:(
      猜你喜欢
      • 2011-06-05
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2013-12-16
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多