Google Maps API v3代码无效

时间:2012-11-05 17:15:18

标签: php javascript google-maps google-maps-api-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;
    }

我该如何解决这个问题。我很沮丧。

1 个答案:

答案 0 :(得分:0)

你的“showMe函数仍在使用API​​ v2语法.GMarker应该是google.maps.Marker,GEvent.addListener应该是google.maps.event.addListener,map.openInfowindow需要针对新语法进行调整,{{ 3}},如下所示(未经测试):

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

地理编码器的回调函数存在同样的问题。