-3

下面的代码用于在 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;
    }

我该如何解决。我对此感到沮丧。

4

1 回答 1

0

您的“showMe 函数仍在使用 API v2 语法。GMarker 应该是 google.maps.Marker,GEvent.addListener 应该是 google.maps.event.addListener,map.openInfowindow 需要针对新语法进行调整,请参阅文档,一些如下所示(未测试):

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);
          }
  }

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

于 2012-11-05T17:35:10.017 回答