【问题标题】:Google Maps API v3 code not workingGoogle Maps API v3 代码不起作用
【发布时间】:2012-10-25 13:54:26
【问题描述】:

以下代码用于在 PHP 页面上显示地图。单击侧栏上特定位置的链接时会显示标记或图钉。单击链接时,地图会缩放到单击链接的位置。我最初是在 V2 中编写代码,现在正尝试将其转换为 V3。地图显示在页面上,但是当我单击链接时,地图不会检索其位置。 当链接被点击时,它会在下面的代码中调用 showme() 函数。

这是 JavaScript 代码。

    var geoCoder = null;
    var marker = null;
    var center = false;
    var show_info = false;

    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(10.4032173, - 61.3719045),
            zoom: 9,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"),
        mapOptions);
    }

  function showAddress(address,description) {
   var infowindow = new google.maps.InfoWindow({content:description});
      geoCoder = new google.maps.Geocoder(); 
      if (geoCoder) {
        geoCoder.getPosition()(
          address,
          function(point) {
            if (point){
              if(center) map.setCenter(point, 13);
              marker = new google.maps.Marker(point);
              google.maps.event.addListener(marker, "click", function() {
                infowindow.open(point, description);
              });
              marker.setMap(marker);
                if(show_info) infowindow.open(point, description);
              }
            }
        );
      }
     geoCoder = null;
    }


    function showLatLng(lat,lng,description) {
            var point=new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
            if (point){
              if(center) map.setCenter(point, 14);
              marker = new google.maps.Marker({position:point,
                                           map: map});

                var infowindow = new google.maps.InfoWindow({content:description});

              google.maps.event.addListener(marker, "click", function() {
                infowindow.open(point, description);
              });
              marker.setMap(maker);
                if(show_info)  infowindow.open(point, description);
              }
      }

    function showme(name, address, lat, lng, contact, Link) {
        var description = desc(name, address, contact, Link);
        center = true;
        show_info = true;
        if (lat || lng) showLatLng(lat, lng, description);
        else showAddress(address, description);
        return false;
    }

    function desc(name, address, contact, Link) {
        //var address = "<div class='googleTip'><div><h1>"+name+"</h1></div><p class='address'>"+address+"<br />"+contact+"<br />"+delivery+"</p><span class='mapLink'></span></div>";
        var Address = ["<div class='googleTip' style='color:#000000; padding:5px'><h3>",
        name, "</h3>", "<div>",
        address, "<br />",
        webLink, "</div></div>", ].join('');
        return Address;
    }

我该如何解决这个问题。我对此感到沮丧。

【问题讨论】:

  • 在 showAddress 函数中,您使用 geo.getPosition 而不是 geoCoder.getPosition。这是问题吗?
  • 没有map.addOverlay(marker);在 V3 中。检查文档。
  • 你也可以删除.join('')之前的最后一个,
  • 版本 3 中应该是 google.maps.event.addListener(...)

标签: php javascript google-maps google-maps-api-3


【解决方案1】:

您的“showMe 函数仍在使用 API v2 语法。GMarker 应该是 google.maps.Marker,GEvent.addListener 应该是 google.maps.event.addListener,map.openInfowindow 需要针对新语法进行调整,@987654321 @,类似于以下内容(未测试):

function showLatLng(lat,lng,description) {
        var point=new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
        if (point){
          if(center_point) map.setCenter(point, 14);
          marker = new google.maps.Marker({position:point,
                                           map: map});
          var infowindow = new google.maps.InfoWindow({content:description});

          google.maps.event.addListener(marker, "click", function() {
            infowindow.open(point, marker);
          });
          marker.setMap(map);
            if(show_info) infowindow.open(point, marker);
          }
  }

地理编码器的回调函数也有同样的问题。

【讨论】:

  • 我已经编辑了原始代码以显示所做的更改。但它仍然无法正常工作。当我点击页面上的链接时,地图没有显示位置。我知道这是 showme() 函数的问题,但我看不到。请帮忙。
  • 你修复了Geocoder 语法吗(我认为没有 getPosition() 方法)?控制台会报告哪些 JavaScript 错误?
  • 我的 google api 控制台没有报告任何错误。我认为这只是一个逻辑错误。
  • 查看google.maps.Geocoder 的文档。带有完整代码和 html 的 jsfiddle 也可能会有所帮助。
  • 我需要在地理编码器中放置请求和回调函数吗?我已经尝试过,但它不起作用。 showme 功能现在也是 V3 格式吗?
猜你喜欢
  • 2013-10-04
  • 1970-01-01
  • 2013-11-04
  • 2013-11-16
  • 2019-07-14
  • 1970-01-01
  • 2012-11-06
  • 2013-04-05
  • 1970-01-01
相关资源
最近更新 更多