0

我有一个 magento 网站,我的产品是企业。我想将谷歌地图添加到产品页面,需要有人帮助我编写代码。

我可以通过键入来拨打邮政编码

<?php echo $_product->getpostcode() ?>

我从互联网上获取了代码,并试图将它放在我的网站上。这是代码:

  <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

   <script>
  var geocoder;
  var map;
  var address = '<?php echo $_product->getpostcode() ?>'
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var mapOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  }

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
      }
    </script>
    <body onload="initialize()">

    <div id="map_canvas" style="width: 500px; height: 300px"></div>

目前它在代码中使用 lat long。我怎样才能将其更改为使用邮政编码?`

4

2 回答 2

1

地图总是使用坐标。这就是地图的工作方式。如果您想使用邮政编码,则需要将其转换为坐标——该过程称为地理编码

您的代码中有一个地理编码功能,目前无法使用,因为您没有名为 的元素address,但稍作调整即可使用。

您当前有一个名为 的全局变量address,其中包含您的邮政编码,因此请让您的地理编码器函数使用它:

在 function 的底部initialize(),添加对 geocoder 函数的调用,将全局变量传递给它:

 map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
 codeAddress(address);
}

更改该函数以接受参数:

function codeAddress(addy) {

然后使用它:

// var address = document.getElementById('address').value;
// The above line needs to be removed; the one below amended
geocoder.geocode( { 'address': addy}, function(results, status) {
于 2012-07-31T09:48:35.643 回答
0
//i have working function use it.
  function initialize() {
var address = "<?php echo $address?>";
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(50.317408,11.12915);
var myOptions = {
  zoom: 8,
  center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("location-canvas"), myOptions);
if (geocoder) {
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
      map.setCenter(results[0].geometry.location);

        var infowindow = new google.maps.InfoWindow(
            { content: '<b>'+address+'</b>',
              size: new google.maps.Size(150,50)
            });

        var marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map, 
            title:address
        }); 
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
        });

      } else {
        alert("Address Not found");
      }
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}

}

于 2014-06-05T05:38:17.133 回答